One veeeery useful and slightly weird thing to add here is that $*, "$*", $@, and "$@" all mean "all the arguments", but get expanded in different ways.
Here's an example:
#!/bin/bash
function print_args() {
echo "-- Unquoted, asterisk --"
for i in $*; do
echo $i
done
echo "-- Quoted, asterisk --"
for i in "$*"; do
echo $i
done
echo "-- Unquoted, atpersand --"
for i in $@; do
echo $i
done
echo "-- Quoted, atpersand --"
for i in "$@"; do
echo $i
done
}
print_args "a" "b c" "d e f"
gives:
-- Unquoted, asterisk --
a
b
c
d
e
f
-- Quoted, asterisk --
a b c d e f
-- Unquoted, atpersand --
a
b
c
d
e
f
-- Quoted, atpersand --
a
b c
d e f
The quoted atpersand ("$@") option is the only option that preserves argument groupings including spaces, while "$*" treats all the arguments as one string, and $* and $@ both expand to the fully split, ungrouped, unquoted arguments.
"$@" behaves as if it were expanded with each element quoted separately, rather than "$*" which behaves as if it were expanded and then the whole expansion quoted.
(Note that this is the same as the behaviour for using arrays, in "${array[*]}" vs "${array[@]}".)
I would agree on weird 😄but interesting nevertheless.
I didn't know about $* nor that quoting/unquoting can have such different results. Going to take note of this, thanks for sharing!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
One veeeery useful and slightly weird thing to add here is that
$*
,"$*"
,$@
, and"$@"
all mean "all the arguments", but get expanded in different ways.Here's an example:
gives:
The quoted atpersand (
"$@"
) option is the only option that preserves argument groupings including spaces, while"$*"
treats all the arguments as one string, and$*
and$@
both expand to the fully split, ungrouped, unquoted arguments."$@"
behaves as if it were expanded with each element quoted separately, rather than"$*"
which behaves as if it were expanded and then the whole expansion quoted.(Note that this is the same as the behaviour for using arrays, in
"${array[*]}"
vs"${array[@]}"
.)I would agree on weird 😄but interesting nevertheless.
I didn't know about
$*
nor that quoting/unquoting can have such different results. Going to take note of this, thanks for sharing!