1. 程式人生 > >[Happy BASH] BASH中的$*和[email protected]的區別

[Happy BASH] BASH中的$*和[email protected]的區別

首先來看一段BASH程式碼:

echo "\[email protected]=" [email protected]
echo "\"\[email protected]\"=" "[email protected]"

echo "print each param from \[email protected]"
for var in [email protected]
do
        echo $var
done

echo "print each param from \"\[email protected]\""
for var in "
[email protected]
" do echo "$var" done echo "print each param from \$*" for var in $* do echo "$var" done echo "print each param from \"\$*\"" for var in "$*" do echo "$var" done

執行./test.sh "a a" b c d,將會得到以下輸出:
[email protected]= a a b c d
"[email protected]
"= a a b c d print each param from [email protected] a a b c d print each param from "[email protected]" a a b c d print each param from $* a a b c d print each param from "$*" a a b c d
總結,總是要用"[email protected]" (雙括號括起來的[email protected]),所有引數的串聯字串,則用"$*" (仍然是雙括號括起來的$*)。