本节所介绍的特殊变量包括:"$0、$1、$2、$@、$*、$#、$$、$?”
直接上干货,后面有各特殊变量的解释:
[root@localhost script]# vim spevar.sh
[root@localhost script]# cat spevar.sh
#!/bin/bash
echo "File Name: $0"
echo "First Parameter: $1"
echo "Second Parameter: $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters: $#"
[root@localhost script]# chmod +x spevar.sh
[root@localhost script]# ./spevar.sh test1 test2
File Name: ./spevar.sh
First Parameter: test1
Second Parameter: test2
Quoted Values: test1 test2
Quoted Values: test1 test2
Total Number of Parameters: 2
$0:表示脚本文件的执行路径,大多数情况会列出脚本的名字
$1:表示传给脚本的第一个参数
$2:表示传给脚本的第二个参数
$@和$*:表示传给脚本的所有参数
$#:表示传给脚本参数的数量
chmod +x spevar.sh:给脚本加执行权限
./spevar.sh test1 test2:执行脚本,并给脚本传两个参数
[root@localhost script]# echo $$
2654
[root@localhost script]# echo $?
0
[root@localhost script]# LS
bash: LS: command not found...
Similar command is: 'ls'
[root@localhost script]# echo $?
127
$$: 表示当前shell的进程id
$?:返回上条命令的执行状态,返回结果为0,表示上条命令正确执行,返回结果非0,表示上条命令执行错误
快乐学习,不足之处,欢迎指出,创作不易,转载请注明出处,谢谢