常用的一些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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值