我们先来写一个简单的脚本,执行以后再看各个变量的意义。
[root@centos7 ~]#vim r1.sh
#! /bin/bash
# ------------------------------------------
# Filename: r1.sh
# ...... //省略
# ------------------------------------------
printf "The complete list is %s\n" "$$" //Shell本身的PID(ProcessID)
printf "The complete list is %s\n" "$!" //Shell最后运行的后台Process的PID
printf "The complete list is %s\n" "$?" //最后运行的命令的结束代码(返回值)
printf "The complete list is %s\n" "$*" //所有参数列表。如"$*"用「"」括起来的情况、以"$1 $2 … $n"的形式输出所有参数。
printf "The complete list is %s\n" "$@" //所有参数列表。如"$@"用「"」括起来的情况、以"$1" "$2" … "$n" 的形式输出所有参数。
printf "The complete list is %s\n" "$#" //添加到Shell的参数个数
printf "The complete list is %s\n" "$0" //Shell本身的文件名
printf "The complete list is %s\n" "$1" //添加到Shell的各参数值。$1是第1参数、$2是第2参数…。
printf "The complete list is %s\n" "$2" //添加到Shell的各参数值。$1是第1参数、$2是第2参数…。
执行结果:
[root@centos7 ~]#./r1.sh 123456 QQ
The complete list is 5327
The complete list is
The complete list is 0
The complete list is 123456 QQ
The complete list is 123456
The complete list is QQ
The complete list is 2
The complete list is ./r1.sh
The complete list is 123456
The complete list is QQ