This matters in shell scripts: for example the script testargs.sh
#! /bin/bash -p
echo $#
for i in $(seq 1 $#)
do
echo "$i: ${!i}"
done
for val in "$@"; do
echo "in quote @, $val"
done
for val in "$*"; do
echo "in quote *, $val"
done
for val in $@; do
echo "not in quote @, $val"
done
for val in $*; do
echo "not in quote *, $val"
done
If this script is executed as /tmp/testargs.sh a b c 'd e'
,
the results are:
4
1: a
2: b
3: c
4: d e
in quote @, a
in quote @, b
in quote @, c
in quote @, d e
in quote *, a b c d e
not in quote @, a
not in quote @, b
not in quote @, c
not in quote @, d
not in quote @, e
not in quote *, a
not in quote *, b
not in quote *, c
not in quote *, d
not in quote *, e
Thus, if number of arguments are to be preserved, always use "$@" or iterate through each argument using the for i in $(seq 1 $#)
loop. Without quotes, both are same.