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

 

摘抄自: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!!!
 
 
set关键字使用:
# ! /bin/bash
# -e: 错误敏感; -u: 使用未定义的变量,抛出异常
# -x: 显示执行的命令
set -eu echo ${PARAM} ./test.sh
./test.sh: line : PARAM: unbound variable echo $?

参考资料:

shell 脚本中$$,$#,$?分别代表什么意思?:http://zhidao.baidu.com/link?url=0KXS2kbSzOsL6egl-ng68W8x5oHNwcqEcP13o1gM-M-dB9Ak6dyESfbIm2B67Q7clsbU7Ypcz3mKyLDzrAwOqK

linux中shell变量$#,$@,$0,$1,$2的含义解释:http://www.cnblogs.com/fhefh/archive/2011/04/15/2017613.html

shell set的几个常见的用法http://blog.chinaunix.net/uid-26335251-id-3330944.html

set -x与set +x指令:http://www.2cto.com/os/201304/205118.html

 

随机推荐

  1. 【JBPM4】完成任务

    示例代码: ProcessEngine processEngine = Configuration.getProcessEngine(); TaskService taskService = proc ...

  2. bzoj 1444 AC自动机 + 矩阵乘法 | 高斯消元

    恶补了一下AC自动机,花了一天时间终于全部搞明白了. 思路:将每个人的串加入AC自动机,在AC自动机生成的状态图上建边,注意单词末尾的节点只能转移到自己概率为1, 然后将矩阵自乘几十次后误差就很小了, ...

  3. C++中正确使用PRId64

    http://blog.csdn.net/win_lin/article/details/7912693

  4. Java工具类-验证码工具

    1.工具类,生成随机验证码字符串 import java.util.Arrays; /** * 工具类,生成随机验证码字符串 * * @version 1.0 * @author * */ publi ...

  5. 有一种感动叫ACM(WJMZBMR在成都赛区开幕式上的讲话)

    各位选手,各位教练,大家好,我是来自清华大学交叉信息学院的陈立杰,今天很荣幸站在这里代表全体参赛选手发言.对于我来说,这是我第一次正式参加ACM的比赛.不过我跟ACM之间的缘分,大概在很早的时候就已经 ...

  6. maven "Generating project in Batch mode"问题的解决

    在maven的五分钟入门里面,有这样一个命令: mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -Darc ...

  7. Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana(分块)

    E. GukiZ and GukiZiana time limit per test 10 seconds memory limit per test 256 megabytes input stan ...

  8. Xamarin iOS项目找不到模拟器

    Xamarin iOS项目找不到模拟器 在Visual Studio中,突然找不到模拟器,并且项目提示试用期过期:Trial period has expired.出现这种情况,是由于苹果系统中Xam ...

  9. SD 一轮集训 day4 弦形袋鼠

    可以发现把每一个 a[i] * b[i] 加到矩阵里去,就相当于 把一个 1*m 的向量伸缩后变成 n个再加到矩阵里去,所以答案就是远=原矩阵中线性线性无关组的个数. (而且好像一个矩阵横着消元和竖着 ...

  10. 浙南联合训练赛 H - The number of positions

    Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say ...