In shell script, $* and [email protected] Both get all command-line arguments, but there is a slight difference between the two in use, the difference is mainly in the use of double quotes, that is, the direct use of $*,[email protected], or use "$*", "[email protected]".
Direct use of $*,[email protected]
#!/bin/Bashcount=1 forParaminch$*; Do Echo "\$* Parameter # $count = $param"Count=$[$count +1 ] DoneCount=1 forParaminch[Email protected]; Do Echo "\[email protected] Parameter # $count = $param"Count= $[$count +1 ] Done
If you execute the script and enter a B C d command-line argument, the For loop output using $* and [email protected] is the same:
The reason is that after the shell has replaced the parameters, two for loops are the same, the following is the form of the script after the parameter substitution:
#!/bin/Bashcount=1 forParaminchA b c D; Do Echo "\$* Parameter # $count = $param"Count=$[$count +1 ] DoneCount=1 forParaminchA b c D; Do Echo "\[email protected] Parameter # $count = $param"Count= $[$count +1 ] Done
"$*" using double quotes, "[email protected]"
#!/bin/Bashcount=1 forParaminch "$*"; Do Echo "\$* Parameter # $count = $param"Count=$[$count +1 ] DoneCount=1 forParaminch "[email protected]"; Do Echo "\[email protected] Parameter # $count = $param"Count= $[$count +1 ] Done
Also input parameter a B c D, the result of the operation is as follows:
The reason is that "$*" is replaced by "a B C D", and the normal "$variable" no difference, and "[email protected]" after the parameter substitution is very special, become "a" "B" "C" "D". In other words, "$*" is replaced with "$ $ ... $N" and "[email protected]" is replaced by "$" "$" ... "$N", the former all parameters enclosed in double quotation marks, the shell as a parameter treatment, double quotation marks inside the parameters are still separated by a space, the latter each argument is surrounded by double quotation marks, parameters and parameters are separated by a space. Therefore, the script is replaced with the following form:
#!/bin/Bashcount=1 forParaminch "a b c D"; Do Echo "\$* Parameter # $count = $param"Count=$[$count +1 ] DoneCount=1 forParaminch "a" "b" "C" "D"; Do Echo "\[email protected] Parameter # $count = $param"Count= $[$count +1 ] Done
$* in shell script and [email protected]