Overview
$0 | The current name of the script file |
$n | Arguments passed to the script or function. n is a number that represents the number of parameters. For example, the first parameter is $ 1, $ 2 is the second parameter. |
$# | The number of arguments passed to the script or function |
$* | Passed to the script or function of all parameters |
$@ | All parameters passed to the script or function. When the double quotes ( "") contains, and $ * is slightly different, the following will be mentioned |
$? | Exit status of the previous command, or a return value of the function |
$- | Shell used to display the current option, see the set command |
$$ | Shell current process ID. For Shell scripts, these scripts are located is the process ID |
$! | Finally, a process running in the background ID number |
Command line arguments ($ n)
The parameters passed to the script called command-line parameters when you run the script. Command-line arguments $ n-expressed, e.g., $ 1 represents the first parameter, $ 2 represents the second parameter, and so on.
Test script:
#!/bin/bash echo "File Name: $0" echo "First Parameter : $1" echo "First Parameter : $2" echo "Quoted Values: $@" echo "Quoted Values: $*" echo "Total Number of Parameters : $#"
Return result:
$. / Test.sh Zara Ali File Name : ./test.sh First Parameter : Zara Second Parameter : Ali Quoted Values: Zara Ali Quoted Values: Zara Ali Total Number of Parameters : 2
The difference between the $ * and $ @
$ * $ @ And all parameters are passed to represent the function or script, "included, they are not to be double quotation marks (") "Output parameter All forms $ 1" "$ 2" ... "$ n" is.
However, when they are in double quotes ( "") included, "$ *" resets all of the parameters as a whole, to "$ 1 $ 2 ... $ n " output in the form of all parameters; "separate $ @" will each parameter, All output parameters in the form of "$ 1" "$ 2" ... "$ n" is.
For ( "") in front of Nonparametric double quotes must be added with the $ symbol '\'
The following example can clearly see the difference between the $ @ $ * and
#!/bin/bash echo "\$*=" $* echo "\"\$*\"=" "$*" echo "\$@=" $@ echo "\"\$@\"=" "$@" echo "print each param from \$*" for var in $* do echo "$var" done echo "print each param from \$@" for var in $@ do echo "$var" done echo "print each param from \"\$*\"" for var in "$*" do echo "$var" done echo "print each param from \"\$@\"" for var in "$@" do echo "$var" done
Performing ./test.sh "a" "b" "c" "d", see the following results:
$*= a b c d "$*"= a b c d $@= a b c d "$@"= a b c d print each param from $* a b c d print each param from $@ a b c d print each param from "$*" a b c d print each param from "$@" a b c d
Exit status ($?)
$? Exit status can be obtained on a command. The so-called exit status, that is, after the previous command returns the result.
Exit status is a number, under normal circumstances, most of the command is successful returns 0, failure to return 1.
However, there are some command returns other values, represent different types of errors.
The following example, the command is successful:
$. / Test.sh Zara Ali File Name : ./test.sh First Parameter : Zara Second Parameter : Ali Quoted Values: Zara Ali Quoted Values: Zara Ali Total Number of Parameters : 2 $echo $? 0 $
$? Multi-preset command to determine whether complete
E.g:
if [ $? -ne 0 ] then # Script does not exit properly exit 1 as