Shell Special variables
1. Special variable names and meanings
Variable name meaning
$ A script name
Location parameter #1
$-$9 positional parameter #2-#9
${10} positional Parameters #10
$# number of positional parameters sh test.sh XX YY, $ #就是2 is the number of parameters represented
$? After the last command executes, the result of executing the command status is returned, or the return value of the function
"$*" all positional parameters (as a single string) *
"[Email protected]" All positional parameters (each as a separate string)
$$ the current shell process ID.
${#*} The number of command-line arguments passed to the script
${#@} The number of command-line arguments passed to the script
$_ the last parameter of the previous command
$! Process ID (PID) of the last job running in the background
2. "Example"
[Email protected] ~]# more third.sh
#!/bin/bash
#parm
echo "This is Shellname: $"
echo "This is Parm1: $"
echo "This is Parm2: $"
echo "Show parm number: $#"
echo "Show Parm list: $*"
echo "Show Parm list: [email protected]"
echo "Show parm number: ${#*}"
echo "Show parm number: ${#@}"
echo "Show process ID: $$"
Operation Result:
[Email protected] ~]# sh third.sh no1var No2var
This is shellname:third.sh
This is Parm1:no1var
This is Parm2:no2var
Show Parm Number:2
Show Parm List:no1var No2var
Show Parm List:no1var No2var
Show Parm Number:2
Show Parm Number:2
Show Process id:23650
[Email protected] ~]# echo $_
No2var
the difference between 3.$* and [email protected]
[Email protected] $* only when it's wrapped in double quotes.
This part is seen on the internet, the original has no idea who is. (This example is very illustrative)
To be precise, the two differ only in the "" (double quotation marks), otherwise, all parameters are indicated (except for $ A).
Well, for example:
SH my.sh p1 "P2 p3" P4 words,
Whether it's [email protected] or $*, you can get P1 P2 p3 P4.
However, if placed in the words "" (double quotes):
"[Email protected]" can be "P1" "P2 P3" "P4" the three different word segments (word);
"$*" can be "P1 P2 p3 P4" This whole string of single word segments.
We can modify the previous my.sh so that the contents are as follows:
Code:
#!/bin/bash
My_fun () {
echo "$#"
}
Echo ' The number of parameter in ' [email protected] ' is ' $ (my_fun "[email protected]")
Echo ' The number of parameter in ' $* ' is ' $ (my_fun "$*")
Then do the sh my.sh p1 "P2 p3" P4 know [email protected] and $* where the difference is ... ^_^
The number of parameter in "[E-mail protected]" is 3
The number of parameter in "$*" is 1
"[email protected] $* 's example description"
[email protected] ~]# cat A.txt
Aaaaaaa
[email protected] ~]# cat B.txt
bbbbbbb
[email protected] ~]# cat c.sh
#!/bin/sh
Cat $
[email protected] ~]# cat *.txt
Aaaaaaa
bbbbbbb
[Email protected] ~]# sh c.sh *.txt
Aaaaaaa
In C.sh wrote the cat $ why only display content from one file, but if the cat *.txt at the command line displays 2 file information
changing c.sh to Cat [email protected] is normal.
[Email protected] ~]# vim c.sh
#!/bin/sh
Cat [email protected] # (change to $* is also possible)
[Email protected] ~]# sh c.sh *.txt
Aaaaaaa
bbbbbbb
4.$?
[[email protected] ~]# echo ' hostname '
10-222
[[email protected] ~]# echo $?
0
[[email protected] ~]# grep ' CPU cores '/proc/cpuinfo
CPU Cores:1
[[email protected] ~]# echo $?
0
The following sections are described in follow-up content
$? The return value of the function is explained in subsequent chapters
The above is a personal point of summary and understanding, the level is not high, the level of writing is very bad, please greatly forgive me.
Can exchange learning together.
My qq:610851588.
Can join my build group (now very few people, hope slowly more up)
Linux Clusters: 183932302
Python, Shell AC Group: 469094734
This article is from the "Go to the Origin dimension" blog, please make sure to keep this source http://yuanji6699.blog.51cto.com/11568362/1773008
Shell-3.shell Special variables