[email protected] ceshi]# cat test.sh
#!/bin/bash
# test.sh
echo "-------------ISF is set to \"-seperator\ "------------"
Ifs= "-seperator"; # Note that there is a minus sign (-) before Seperator
For i in "[email protected]"; Do echo "@ ' $i '"; Done
For i in "$*"; Do echo "* ' $i '"; Done
echo "-------------ISF is set to null------------------------"
ifs=
For i in "[email protected]"; Do echo "@ ' $i '"; Done
For i in "$*"; Do echo "* ' $i '"; Done
echo "-------------ISF is unset------------------------"
Unset IFS
For i in "[email protected]"; Do echo "@ ' $i '"; Done
For i in "$*"; Do echo "* ' $i '"; Done
Echo '---------$* and [email protected] is not put into double quotes ("")-------'
For i in [email protected]; Do echo "@ ' $i '"; Done
For i in $*; Do echo "* ' $i '"; Done
test.sh AA BB CC performs the following results:
[[Email protected] ceshi]# sh test.sh aa bb cc
-------------ISF is set to "-seperator"------------
@ ' AA '
@ ' BB '
@ ' CC '
* ' AA-BB-CC '
-------------ISF is set to null------------------------
@ ' AA '
@ ' BB '
@ ' CC '
* ' AABBCC '
-------------ISF is unset------------------------
@ ' AA '
@ ' BB '
@ ' CC '
* ' AA bb cc '
---------$* and [email protected] is not put into double quotes ("")-------
@ ' AA '
@ ' BB '
@ ' CC '
* ' AA '
* ' BB '
* ' CC '
Thus
1) when no double quotation marks (""), $*,[email protected] is expanded when the behavior is the same;
2) when $*,[email protected] are placed in double quotation marks ("");
2.1) If the value of the variable IFS is set and the value is not NULL, then $* is expanded with the first character of ${ifs} to concatenate all parameters (except the argument $), which is "$1c$2c$3c ...", where C represents the first character of ${ifs};
2.2) If the variable ifs is empty, then the $* is expanded to simply connect all parameters (except the parameter $), i.e. "$1$2$3 ..."
2.3) If the variable IFS is not defined, the $* is expanded with a space character to connect all parameters (except the parameter $), which is "$ $ ..."
However, the deployment of [email protected] is consistent with the absence of double quotes.
More examples of shell scripts:
#!/bin/bash
Set ' apple pie ' pears peaches
For I in $* #单引号被去掉, loop single character output #
Do Echo $i
Done
[Email protected] ceshi]# sh test1.sh
Apple
Pie
Pears
Peaches
--------------------------------------------------------------
#!/bin/bash
Set ' apple pie ' pears peaches
For i in "$*" #单引号被去掉 but as String once output #
Do Echo $i
Done
[Email protected] ceshi]# sh test2.sh
Apple pie Pears Peaches
-----------------------------------------------------------------
#!/bin/bash
Set ' apple pie ' pears peaches
For i in [email protected] #单引号被去掉, loop single character output #
Do Echo $i
Done
[Email protected] ceshi]# sh test3.sh
Apple
Pie
Pears
Peaches
-----------------------------------------------------------------
#!/bin/bash
Set ' apple pie ' pears peaches
For i in "[email protected]" #每个位置参数都将当成一个加引号的字符串: "Apple pie", "Pears", "Peaches" #
Do Echo $i
Done
[Email protected] ceshi]# sh test4.sh
Apple pie
Pears
Peaches
-----------------------------------------------------------------
test5.sh
Sum=0
for Var
In "[Email protected]"
Do
Let sum=sum+ $var
Done
echo "Sum= $sum"
./test1.sh 1 2 3
The result is sum=6
-----------------------------------------------------------------
test6.sh
Sum=0
for Var
In "$*"
Do
Let sum=sum+ $var
Done
echo "Sum= $sum"
./test2.sh 1 2 3
The result is Sum=1
-----------------------------------------------------------------
Analysis One:
Bash-x test1.sh
The results are as follows:
+ sum=0
+ for Var in ' [Email protected] '
+ Let Sum=sum+1
+ for Var in ' [Email protected] '
+ Let sum=sum+2
+ for Var in ' [Email protected] '
+ Let Sum=sum+3
+ Echo sum=6
Sum=6
You can see that for is executed three times, the for is bounded by "" because each parameter is surrounded by ""
Analysis Two:
Bash-x test2.sh
The results are as follows:
+ sum=0
+ for Var in ' "$*" '
+ Let sum=sum+1 2 3
+ Echo Sum=1
Sum=1
It can be seen that the loop executes only once, stating that the whole of $* is surrounded by "", not every parameter is "", so it only executes once
A more straightforward example is:
for Var
In "a B C"
Do
echo "${var}@"
Done
Print:
A b [email protected]
for Var
In "a" "B" "C"
Do
echo "${var}@"
Done
Print:
[Email protected]
[Email protected]
[Email protected]
This article from "Flylinux" blog, reproduced please contact the author!
Comparison of [email protected] and $* in the shell