Shell Special variables: Shell $, $#, $*, $@, $, $$, and command-line arguments

Source: Internet
Author: User

Shell Special variables: Shell $0, $#, $*, [email protected], $?, $$ and command line arguments special variable list variable meaning $0the file name of the current script $n the arguments passed to the script or function. N is a number that represents the first few parameters. For example, the first argument is a $1, the second argument is a $2. $# the number of arguments passed to the script or function. $*all parameters passed to the script or function. [email protected] All parameters passed to the script or function. Surrounded by double quotes (" ") when included, with $*slightly different, as will be mentioned below. $? The exit state of the last command, or the return value of the function. In general, the success of most command execution will return0, failed to return1. $$ the current shell process ID. For Shell scripts, this is the process ID where the scripts are located. $*and [email protected] the difference $* and [email protected] are all arguments passed to the function or script, not double quotes (" ") is included in the" $" " $"..."$n"all parameters in the form of output. But when they are double-quoted (" ") is included,"$*"All parameters will be used as a whole to"$ $ ... $n"All parameters in the form of output;"[email protected]"Each parameter is separated to" $" " $"..."$n"all parameters in the form of output. The following example can be clearly seen in the $*and [email protected] The difference: #!/bin/BashEcho "\$*="$*Echo "\"\$*\"=" "$*"Echo "\[email protected]="[email protected]Echo "\ "\[email protected]\" =" "[email protected]"Echo "print each param from \$*" forVarinch$* Do    Echo "$var" DoneEcho "print each param from \[email protected]" forVarinch[email protected] Do    Echo "$var" DoneEcho "print each param from \ "\$*\"" forVarinch "$*" Do    Echo "$var" DoneEcho "print each param from \ "\[email protected]\"" forVarinch "[email protected]" Do    Echo "$var" Doneexecution./test.SH "a" "b" "C" "D"and see the following result: $*=a b c D"$*"=a B C d[email protected]=a b c D"[email protected]"=a b c dprint each param from $*abcdprint each param from [email protected]abcdprint to Param from"$*"a b c dprint each param from"[email protected]"ABCD Note: When double quotes are included,"$*"The parameters are treated as a whole, and"[email protected]"or traverse each parameter

Shell Special variables: Shell $, $#, $*, [email protected], $?, $$ and command line arguments

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.