In the book "Linux Shell Scripting Cookbook"
It says $@ expands as $1, $2, $3 and so on and $* expands as $1c$2c$3, where c is the first character of IFS.
What's the difference between $@ and $* and what IFS means?
In the book "Linux Shell Scripting Cookbook"
It says $@ expands as $1, $2, $3 and so on and $* expands as $1c$2c$3, where c is the first character of IFS.
What's the difference between $@ and $* and what IFS means?
IFS is the internal field separator, it basically means what the shell recognises as what seperates words.
So to run the following command
IFS=$'\n'
Would cause the shell to recognise new lines as seperators.
$ is the sign of something being assigned to a variable
However the numbers are reserved for script inputs.
So $1 would be a variable input, $2 would be a second variable input.
$@ is all of the parameters passed to the script.
So if you run the command
bash command.sh bork woof meow
This would be the value of the above listed variables
$1 = bork
$2 = woof
$3 = meow
$@ = bork woof meow
It's hard to improve on the explanation given in the manuals. For instance, dash(1) says:
$@Expands to the positional parameters, starting from one. When the expansion occurs within double-quotes, each positional parameter expands as a separate argument. If there are no positional parameters, the expansion of
@generates zero arguments, even when@is double-quoted. What this basically means, for example, is if$1is “abc” and$2is “def ghi”, then"$@"expands to the two arguments:"abc""def ghi"
We can demonstrate the difference between $* and $@ with some examples:
$ set 1 "2 3"
$ printf '"%s"\n' "$@"
"1"
"2 3"
$ printf '"%s"\n' $@
"1"
"2"
"3"
$ printf '"%s"\n' "$*"
"1 2 3"
$ printf '"%s"\n' $*
"1"
"2"
"3"
The manual page also describes IFS:
IFSInput Field Separators. This is normally set to ⟨space⟩, ⟨tab⟩, and ⟨newline⟩. See the White Space Splitting section for more details.