First, Shell special variables
1. Position Variables
Positional variables
|
Meaning |
$ |
Gets the file name of the currently executing shell script, including the script path |
$n |
Gets the nth parameter of the currently executing shell script, n=1..9, which represents the file name of the script when n is 0, if n is greater than 9, enclosed in curly braces ${10} |
$* |
Get all parameters of the current shell, treat all command-line arguments as a single string, equivalent to "$1$2$3", and note the #的区别: Parameter 1, parameter 2, Parameter 3 |
$# |
Gets the total number of parameters in the current shell command line |
[Email protected] |
All parameters of this program "$" "$" "$" "...", this is the best way to pass parameters to other programs because it preserves any whitespace embedded in each parameter. |
2. Process state variables
Process state variables
|
Meaning |
$$ |
Gets the current shell's process number (PID): 1534 |
$! |
Gets the PID that executes the previous instruction |
$? |
Gets the return value of the last instruction executed |
$_ |
The last parameter of the command or script that was executed before this |
Ii. Examples and explanations
1. Position variables
$ $ Gets the file name of the currently executing shell script, including the script path
[[email protected] ~]# cat 0.shecho $0[[email protected] ~]# sh 0.sh0.sh[[email protected] ~]# sh/root/0.sh/root/0.sh
$n
[[email protected] ~]# cat n.shecho $1[[email protected] ~]# sh n.sh lelelele[[email protected] ~]# sh n.sh lele baobaolele[[email protected] ~]# sh n.sh "Lele baobao" lele baobao[[email protected] ~]# seq 15 | sed ' s#^#$ #g ' |tr "\ n" " " > n1.sh[[email Protected] ~] #vim n1.shecho echo $1 $2 $3 $4 $5 $6 $7 $ 8 $9 $10 $11 $12 $13 $14 $15[[email protected] ~]# sh N1.sh {a. Z}a b c d e f g h i a0 a1 a2 a3 a4 a5 #如果n大于9, enclosed in curly braces ${10}[[email protected] ~]# cat n1.shecho $1  $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11} ${12} ${13} ${14} ${15}[[email protected] ~]# sh n1.sh {a. Z}a b c d e f g h i j k l m n o
$* and $#
[[Email protected] ~]# set--"I am a" good teacher #--end option section, new parameters from "I am" [[email protected] ~]# echo $*i am a good tea Cher[[email protected]ost ~]# echo $ #3
$* and [email protected]
[[Email protected] ~] #set--"I am a" good teacher[[email protected] ~]# for I in "$*"; Do echo $i;d one #在有双引号情况, the parameters quoted as a parameter output I am a good teacher[[email protected] ~]# for i in "[email protected]"; Do echo $i;d one #在有双引号的情况, each parameter independently outputs I am agoodteacher if $* and [email protected] without quotation marks [[email protected] ~]# for i in $*; Do echo $i;d one I ama Goodteacher[[email protected] ~]# for i in [email protected]; do echo $i; Donei AMA Goodteacher
$* treats all command-line arguments as a single string, equivalent to "$1$2$3"
[email protected] treats each parameter of the command line as a separate string, equivalent to "$" "$" "$", which is the best way to pass parameters to other programs because it preserves any whitespace embedded in each parameter.
Note: The above differences are only when double quotes are added, i.e. "$*" "[Email protected]"
2. Process variables
Explain the most commonly used process variables $?
$? gets the return value of the last instruction executed
Value
|
Meaning |
0 |
Command runs successfully |
2 |
Permission denied |
1-125 |
Command run failure, specific exit meaning, is defined by individual commands |
126 |
The command was found, but the file could not be executed |
127 |
Command not Found |
>128 |
command is forced to end by system |
[[email protected] ~]# ll/dev/null crw-rw-rw-1 root root 1, 3 June 9 17:37/dev/null[[email protected] ~]# echo $?0[[ema Il protected] ~]# ll fools:cannot access foo:no such file or Directory[[email protected] ~]# echo $?2
Shell Learning Note 2