经常使用的一些shell变量

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

$# 参数个数函数

$* 全部位置参数做为一个单词code

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

 

$_ 以前执行的命令的最后一个参数字符串

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

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

示例:test

test.sh:awk

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 threefile

输出:

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
相关文章
相关标签/搜索