Reprinted from: http://www.cnblogs.com/davygeek/p/5670212.html
Learn to write a script today encountered some variables do not know, here do the next record.
Variable |
Meaning |
$ |
File name of the current script |
$n |
Arguments passed to the script or function. N is a number that represents the first few parameters. For example, the first parameter is $ $, and the second argument is a $ |
$# |
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. When enclosed by double quotation marks (""), it is slightly different from $* |
$? |
The exit state of the last command, or the return value of the function |
$$ |
The current shell process ID. For Shell scripts, the process ID where these scripts are located |
The difference between $* and [email protected]
$* and [email protected] All represent all parameters passed to a function or script, and are not enclosed by double quotation marks (""), with "$" and "$" ... All parameters are output in the form "$n". But when they are enclosed in double quotation marks (""), "$*" takes all the parameters as a whole and outputs all parameters in the form of "$ $ ... $n"; "[email protected]" separates the various parameters to "$" "$" ... All parameters are output in the form "$n".
Cases:
Create ex4-extend.sh
#!/bin/shecho "\$*=" $*echo "\" \$*\ "=" $* "echo" \[email protected]= "[email Protected]echo" \ "\[email protected]\" = "" [E Mail protected] "echo" Print each param from \$* ' for Var in $*doecho ' $var ' doneecho ' Print each param from \[email protecte D] "for Var in [e-mail Protected]doecho" $var "doneecho" print each param from \ "\$*\" "to Var in" $* "do echo" $var "Doneecho "Print each param from \" \[email protected]\ "" for var in "[Email protected]" Doecho "$var" done
Execute./ex4-extend.sh "a" "B" "C" "D" with the result as follows
$*= 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-param from ' $* ' a b c dprint each param from "[email protected]" ABCD
Exit status
$? You can get the exit status of the previous command. The so-called exit status is the return result after the last command was executed.
Exit status is a number, in general, most of the command execution succeeds returns 0, and the failure returns 1.
However, there are some commands that return other values that represent different types of errors.
Shell script $# $ [email protected] $* $$ $! The meaning of the $?