Not a long question, what does this mean?
LogMsg "File:${@}"
LogMsg() is a method that logs a message with a timestamp.
But what the heck does
${@}
mean? I should also mention the script also has $1 and $2 as well. Google produces no results.
Not a long question, what does this mean?
LogMsg "File:${@}"
LogMsg() is a method that logs a message with a timestamp.
But what the heck does
${@}
mean? I should also mention the script also has $1 and $2 as well. Google produces no results.
Literally:
f() { printf '%s\n' "File: $@"; }
f "First Argument" "Second Argument" "Third Argument"
will expand to and run the command:
printf '%s\n' "File: First Argument" "Second Argument" "Third Argument"
That is to say: It expands your argument list ($1, $2, $3, etc) while maintaining separation between subsequent arguments (not throwing away any information provided by the user by way of quoting).
This is different from:
printf '%s\n' File: $@
or
printf '%s\n' File: $*
which are both the same as:
printf '%s\n' "File:" "First" "Argument" "Second" "Argument" "Third" "Argument"
...these both string-split and glob-expand the argument list, so if the user had passed, say, "*" (inside quotes intended to make it literal), the unquoted use here would replace that character with the results of expanding it as a glob, ie. the list of files in the current directory. Also, string-splitting has other side effects such as changing newlines or tabs to spaces.
It is also different from:
printf '%s\n' "File: $*"
which is the same as:
printf '%s\n' "File: First Argument Second Argument Third Argument"
...which, as you can see above, combines all arguments by putting the first character in IFS (which is by default a space) between them.
in KSH there is two positional paremeters * and @
"$*" is a single string that consists of all of the positional parameters, separated by the first character in the variable IFS (internal field separator), which is a space, TAB, and newline by default.
On the other hand, "$@" is equal to "$1" "$2" … "$N ", where N is the number of positional parameters.
For more detailed information and example : http://oreilly.com/catalog/korn2/chapter/ch04.html
This is the set of the arguments of the command line.
If you launch a script via a command like cmd a b c d, there is 5 arguments, $0 will be the command cmd, $1the first argument a, $2 the second b, etc. ${@} will be all the arguments except the command.
The one piece that was not explained by the other posts is the use of {. $@ is the same as ${@} but allows you to add letters, etc if needed and those letters will not have a space added in. e.g. you could say ${foo}dog and if $foo was set to little the result would be littledog with no spaces. In the case of ${@}dogdog and $@ is set to a b c d the result is "a" "b" "c" "ddogdog".