$* and $@ in shell script

Source: Internet
Author: User

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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.