Shell脚本中$#,$@,$0,$1,$2等变量的含义

发布时间:2024-04-02 点击:96
在看脚本的时候经常可以看到$#,$@,$0,$1,$2等变量,那么这些是什么意思呢?博主摘录一些常用的做下解释。
$$
shell本身的pid(processid)
$!
shell最后运行的后台process的pid
$?
最后运行的命令的结束代码(返回值)
$-
使用set命令设定的flag一览
$*
所有参数列表。如”$*”用「”」括起来的情况、以”$1 $2 … $n”的形式输出所有参数。
$@
所有参数列表。如”$@”用「”」括起来的情况、以”$1″ “$2” … “$n” 的形式输出所有参数。
$#
添加到shell的参数个数
$0
shell本身的文件名
$1~$n
添加到shell的各参数值。$1是第1参数、$2是第2参数…
举例:
#!/bin/bash
#
printf “the complete list is %s\\n” “$$”
printf “the complete list is %s\\n” “$!”
printf “the complete list is %s\\n” “$?”
printf “the complete list is %s\\n” “$*”
printf “the complete list is %s\\n” “$@”
printf “the complete list is %s\\n” “$#”
printf “the complete list is %s\\n” “$0”
printf “the complete list is %s\\n” “$1”
printf “the complete list is %s\\n” “$2
结果:
[aric@localhost ~]$ bash params.sh 123456 qq
the complete list is 24249
the complete list is
the complete list is 0
the complete list is 123456 qq
the complete list is 123456
the complete list is qq
the complete list is 2
the complete list is params.sh
the complete list is 123456
the complete list is qq
参考资料:《advanced bash-scripting guide.pdf》:http://download.chekiang.info/blog/advanced%20bash-scripting%20guide.pdf

购买的ssl证书如何使用?SSL证书有什么优点?
企业该如何选择可绑域名免费空间?
香港云服务器去哪里买
.io域名爆百万交易,.io域名魅力在哪?
SEO能为企业带来什么价值?
有云服务器ecs为什么还要rds
阿里云服务器怎么设置内网
app手机网站开发需要注意什么问题?app手机网站开发有什么流程?