Parsing/passing command line arguments to a bash script - what is the difference between “$@” and “$*”?

前端 未结 3 1033
臣服心动
臣服心动 2020-12-10 06:18

I am using a bash script to call and execute a .jar file from any location without having to constantly enter its explicit path.

The .jar r

3条回答
  •  醉话见心
    2020-12-10 06:48

    $* contains a single string composed of all of the arguments separated by the first character of $IFS. Thus, if you set IFS to contain a character that cannot usefully be present in your arguments (e.g. a newline if you're passing filenames), then you can consider $* and "$@" to be functionally equivalent, though they work differently under the hood.

    $@ is essentially an array of all of the arguments. When passed in double quotation marks ("$@"), the shell individually wraps each value in the array with double quotes (making it an odd exception to the normal rule of double quotes wrapping the entire expansion of a variable).

    IIRC, the behavior of a bare $@ without double quotes is not fully specified in the POSIX spec, so it should be considered undefined behavior. Thus, you should never use a bare $@ without double quotes.

    BTW, if you care about System V prior to version 3, there were some ancient quirks in how they handled $@. These are largely of historical interest at this point, but they'll give you some appreciation of how the standard evolved as people ran into problems. :-)

提交回复
热议问题