常用的一些shell变量

$0 $1 表示第几个参数,在awk中以$1开始计

$# 参数个数

$* 所有位置参数作为一个单词

$@ 与$*同义,但每一个参数都是一个独立的“引用字符串”,推荐使用$@

$_ 之前执行的命令的最后一个参数

$? 命令、函数或者脚本本身的退出状态

$$ 脚本自身的PID,可用于构造一个“unique”的临时文件名

示例:

test.sh:

echo "the total number of parameters is $#"
echo "the name of shell file is $0"
echo "the total parameters using $*: $*"
echo "the total parameters using $@: $@"
echo "arg in "$*""
for arg in "$*"
do
    echo $arg
done

echo "arg in "$@""
for arg in "$@"
do
    echo $arg
done

echo "the last parameter is $_"
echo "the shell pid id $$"

运行:chmod 777 test.sh ; ./test.sh one two three

输出:

the total number of parameters is 3
the name of shell file is ./test.sh
the total parameters using $*: one twothree
the total parameters using $@: one twothree
arg in "$*"
one two three
arg in "$@"
one
two
three
the last parameter is three
theshell pid id 27635


原文地址:https://www.cnblogs.com/OpenLinux/p/5020692.html