欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

浅谈linux中shell变量$#,$@,$0,$1,$2的含义解释

程序员文章站 2022-07-04 18:30:23
摘抄自:abs_guide 下载地址:http://www.tldp.org/ldp/abs/abs-guide.pdf linux中shell变量$#,$@,$0,$...

摘抄自:abs_guide

下载地址:http://www.tldp.org/ldp/abs/abs-guide.pdf

linux中shell变量$#,$@,$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参数…。

示例:

1 #!/bin/bash
 2 #
 3 printf "the complete list is %s\n" "$$"
 4 printf "the complete list is %s\n" "$!"
 5 printf "the complete list is %s\n" "$?"
 6 printf "the complete list is %s\n" "$*"
 7 printf "the complete list is %s\n" "$@"
 8 printf "the complete list is %s\n" "$#"
 9 printf "the complete list is %s\n" "$0"
10 printf "the complete list is %s\n" "$1"
11 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
have a nice day!!!

以上这篇浅谈linux中shell变量$#,$@,$0,$1,$2的含义解释就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。