Linux Shell the N kinds of usage '$' symbol

In the Shell $is a special character, have different uses in different scenarios.

Reference variable

Use $direct reference variables, including loop variable.

1
2
3
root@iZ2ze43t8c5urajez7ws4eZ:~# x=1
root@iZ2ze43t8c5urajez7ws4eZ:~# echo $x
1

Double quotes "string enclosed support variable interpolation.

1
2
3
root@iZ2ze43t8c5urajez7ws4eZ:~# x=1
root@iZ2ze43t8c5urajez7ws4eZ:~# echo "x = $x"
x = 1

Use ${}as a word boundary.

1
2
3
root@iZ2ze43t8c5urajez7ws4eZ:/var/log/nginx# x=1
root@iZ2ze43t8c5urajez7ws4eZ:/var/log/nginx# echo "x = ${x}yz"
x = 1yz

Use $ {#} acquisition variable length string.

1
2
3
root@iZ2ze43t8c5urajez7ws4eZ:/var/log/nginx# s=hello
root@iZ2ze43t8c5urajez7ws4eZ:/var/log/nginx# echo "s.length = ${#s}"
s.length = 5

The script that references or function parameters

Based on the subject under reference, 0 represents the shell script file name, n represents the n argument starting at 1, the first argument is $ 1.

1
2
3
4
5
root@iZ2ze43t8c5urajez7ws4eZ:~# echo 'echo $0 $1' > test.sh
root@iZ2ze43t8c5urajez7ws4eZ:~# cat test.sh
echo $0 $1
root@iZ2ze43t8c5urajez7ws4eZ:~# sh test.sh 1 2 3
test.sh 1

Note that single quote 'string enclosed not interpolated.

Use $#to get the script or number of arguments.

1
2
3
root@iZ2ze43t8c5urajez7ws4eZ:~# echo 'echo $#' > test.sh
root@iZ2ze43t8c5urajez7ws4eZ:~# sh test.sh 1 2 3
3

Use $@or $*reference list of arguments as an array. The difference is that with the use of double quotation marks, it is assumed the incoming parameters 1 2 3, then the "$@"value of "1", "2", "3" three variables, $*a value of "1 2 3" is a variable.

test.sh

1
2
3
4
5
6
7
8
9
10
echo using '$@'
for x in "$@"
do
echo + $x
done
echo using '$*'
for x in "$*"
do
echo - $x
done
1
2
3
4
5
6
7
root @ iZ2ze43t8c5urajez large column   Linux Shell the N kinds of usage '$' symbol 7ws4eZ: ~ # 2. 3. 1 SH test.sh 
the using $ @
+. 1
+ 2
+. 3
the using $ *
-. 3. 1 2

The return value of the last command

Use $?the return value of a command. 0 indicates a successful execution, non-zero value indicates an error, note differs from the C language here.

$?The easy to remember name, query presents to ask OS, how about the result of last executed command it? Note that each execute a command will override this variable.

1
2
3
4
5
6
root@iZ2ze43t8c5urajez7ws4eZ:~# true
root@iZ2ze43t8c5urajez7ws4eZ:~# echo $?
0
root@iZ2ze43t8c5urajez7ws4eZ:~# false
root@iZ2ze43t8c5urajez7ws4eZ:~# echo $?
1

Execute commands and get output

Use $()executed and acquires command output assigned to variables, functions equivalent to double quotes.

1
2
3
4
root@iZ2ze43t8c5urajez7ws4eZ:~# echo `date`
Sat Dec 2 15:07:50 CST 2017
root@iZ2ze43t8c5urajez7ws4eZ:~# echo $(date)
Sat Dec 2 15:07:56 CST 201

Expression evaluation

Use $[]of the expression is evaluated, and the exprcommand is different, $[]for the interpolation, and exprthe value is output.

1
2
3
4
5
6
root@iZ2ze43t8c5urajez7ws4eZ:~# $[ 1 + 1 ]
2: command not found
root@iZ2ze43t8c5urajez7ws4eZ:~# echo $[ 1 + 1 ]
2
root@iZ2ze43t8c5urajez7ws4eZ:~# expr 1 + 1
2

获取当前进程ID

使用$$获取当前进程ID。

1
2
root@iZ2ze43t8c5urajez7ws4eZ:~# echo $$
16150

后台运行的最后一个进程ID

使用$!获取后台运行的最后一个进程ID,在命令后面使用&即可以创建后台进程。

1
2
3
4
5
root@iZ2ze43t8c5urajez7ws4eZ:~# tail -f /var/log/nginx/access.log &
[1] 16176
root@iZ2ze43t8c5urajez7ws4eZ:~# echo $!
16176
root@iZ2ze43t8c5urajez7ws4eZ:~# kill $!

获取shell选项

使用$-获取当前shell的选项。具体的选项意义可以参考segmentfault上的回答

1
2
root@iZ2ze43t8c5urajez7ws4eZ:~# echo $-
himBH

Guess you like

Origin www.cnblogs.com/wangziqiang123/p/11712266.html