[lidapeng@X61s ~]$ cat 1.sh
echo $#
echo "$*"
echo "$@"
echo "print each param from \"\$*\""
for var in "$*"
do
echo "$var"
done
echo "print each param from \"\$@\""
for var in "$@"
do
echo "$var"
done
[lidapeng@X61s ~]$bash 1.sh a b c
3
a b c
a b c
print each param from "$*"
a b c
print each param from "$@"
a
b
c
特殊状态变量:
$?
上一次命令执行状态返回值,0表示正确,其他值表示失败
$$
当前shell脚本的进程号
$!
上一次后台进程的PID
$_
获取上次命令的最后一个参数
实例:
[lidapeng@X61s ~]$ cat 2.sh
[ $# -ne 2 ] && { -ne 表示不等于
echo "must be two args"
exit 119 终止程序,并返回状态码119,用于echo $?返回值
}
echo ok
echo "当前脚本的id是: $$"
[lidapeng@X61s ~]$ bash 2.sh 1 2 3
must be two args
[lidapeng@X61s ~]$ bash 2.sh 1 2
ok
当前脚本的id是:46854
[lidapeng@X61s ~]$ echo $_
2
[lidapeng@X61s ~]$ bash 2.sh 1
must be two args
[lidapeng@X61s ~]$ echo $?
119
[lidapeng@X61s ~]$ echo $_
119