shell脚本中的部分环境变量$0,$?,$!,$$,$*,$#,$@

  • $$
    shell本身的PID(ProcessID)
  • $!
    shell最后运行的后台Process的PID
  • $?
    最后运行结束的命令的返回状态值,为0则为执行成功,为1则为执行失败
  • $*
    所有参数列表.以"$1 $2 … $n"的形式输出所有参数
  • $@
    所有参数列表.以"$1" " 2 " . . . " 2" ... " 2"..."n"的形式输出所有参数
  • $#
    添加到shell的命令行参数个数
  • $0
    脚本本身的文件名
  • $1 ~ $n
    添加到脚本的各个参数值,$1是第一个参数,$2是第二个参数…

给出一个例子辅助理解

#创建一个脚本测试文件
[root@localhost ~]# vim test.sh
echo "number:$#"
echo "scname:$0"
echo "first:$1"
echo "second:$2"
echo "parameter:$@"
[root@localhost ~]# chmod +x test.sh		#添加执行权限
[root@localhost ~]# ./test.sh  aa  bb		#执行当前目录下的test.sh脚本并附带aa bb两个参数
#结果为:
number:2
scname:./test.sh
first:aa
second:bb
parameter:aa bb

猜你喜欢

转载自blog.csdn.net/qq_42428671/article/details/105885693