您的位置:首页 > 运维架构 > Shell

shell 脚本之特殊变量字符

2016-11-30 09:45 573 查看

shell 脚本之特殊变量字符

shell 脚本中我们知道变量都是以$符号开头的,但是有一些特殊的变量也很有用,这里我们就介绍一下有哪些比较实用的。

$0 这个脚本的执行名字
--------------------------
#! /bin/bash
echo $0
--------------------------
[root@localhost performance]# ./test.sh
./test.sh


$n 这个程式的第n个参数值,n=1..9
-----------------------------------------
[root@localhost performance]# cat test.sh
#! /bin/bash
echo $1 $2 $3
-----------------------------------------
[root@localhost performance]# ./test.sh hello world 123
hello world 123


$* 这个程式的所有参数,此选项参数可超过9个。 (所有参数组成一个字符串)
----------------------------------------------------------
#! /bin/bash
echo $*
----------------------------------------------------------
[root@localhost performance]# ./test.sh I love shell
I love shell


$@ 跟$*类似,但是可以当作数组用  (返回多个参数字符串)
------------------------------------------------
#! /bin/bash
echo $@
------------------------------
[root@localhost performance]# ./test.sh I love shell
I love shell


$# 这个程式的参数个数
------------------
#! /bin/bash
echo $#
------------------
[root@localhost performance]# ./test.sh I love shell
3


$? 执行上一个指令的返回值 (显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误)
------------------------------------------------
#! /bin/bash
cd abc #打开不存在目录
echo $?
cd ./ #打开当前目录
echo $?
------------------------------
[root@localhost performance]# ./test.sh
./test.sh: line 2: cd: abc: No such file or directory
1
0


$$ 这个程式的PID(脚本运行的当前进程ID号)
-----------------------------------
#! /bin/bash
echo $$
-----------------------------------
[root@localhost performance]# ./test.sh I love shell
22508


$! 执行上一个背景指令的PID(后台运行的最后一个进程的进程ID号)
$- 显示shell使用的当前选项,与set命令功能相同
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell 脚本