0

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.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
R4F6
  • 782
  • 1
  • 9
  • 26
  • http://stackoverflow.com/questions/3811345/how-to-pass-all-arguments-passed-to-my-bash-script-to-a-function-of-mine – RBH Oct 17 '14 at 16:52

4 Answers4

4

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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
2

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

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
1

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.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • Like the entire line of what was passed in to the script? – R4F6 Oct 17 '14 at 16:53
  • @R4F6, yes -- split out as separate arguments (if quoted as `"$@"`), vs `$*` which combines them together. Using `$@` without quotes is just the same as `$*`, and thus typically a bug. – Charles Duffy Oct 17 '14 at 16:58
0

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".

pedz
  • 2,271
  • 1
  • 17
  • 20