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. git reset用法

    git 删除 错误 提交的 commit 方法:         根据–soft –mixed –hard,会对working tree和index和HEAD进行重置:    git reset -- ...

  2. cocos2d-x addImageAsync()异步加载资源成功之后的场景跳转问题

    http://blog.csdn.net/w20175357/article/details/23546985 1.先说说addImageAsync()异步加载图片的问题 做游戏的时候现在资源的比较大 ...

  3. 【ASP.NET】IHttpHandler和IHttpModule

    上篇文章我们主要讲了HttpApplicatiion管道事件,那么我么如何处理这些管道事件呢,以及请求在ASP.NET是如何执行的呢,我们来了解一下IHttpHandler和IHttpModule 引 ...

  4. POJ 1200 Crazy Search【Hash入门】

    RK法:https://www.cnblogs.com/16crow/p/6879988.html #include<cstdio> #include<string> #inc ...

  5. CodeForces 731D 80-th Level Archeology

    区间并. 对于上下两个数字,如果不一样,那么可以计算出哪一段范围内可以保证字典序,并且后面所有位置都无需再考虑.对所有范围求交集就是答案了. 求交集写起来有点烦,直接对不可取的范围求并即可. #pra ...

  6. 洛谷——P1068 分数线划定

    P1068 分数线划定 题目描述 世博会志愿者的选拔工作正在 A 市如火如荼的进行.为了选拔最合适的人才,A 市对 所有报名的选手进行了笔试,笔试分数达到面试分数线的选手方可进入面试.面试分数线根 据 ...

  7. Friends number NBUT - 1223 (暴力打表)

    Paula and Tai are couple. There are many stories between them. The day Paula left by airplane, Tai s ...

  8. 关于oracle存储过程需要注意的问题

    在使用oracle存储过程时,有一些需要注意的地方,下面就来总结一下. 1.在oracle的存储过程中,数据表别名不能加as 也许是为了区分存储过程中的as,怕与过程中的as冲突. 如: select ...

  9. 【计算几何】【圆反演】计蒜客17314 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 G. Finding the Radius for an Inserted Circle

    题意:给你三个半径相同的圆,它们切在一起,然后让你往缝里一个一个地塞圆,问你塞到第k个的半径是多少. 就把上面那两个圆的切点当成反演中心,然后会反演成这个样子,两个平行直线和一个圆. 然后就是往那个圆 ...

  10. 【数论】【欧拉函数】CDOJ1724 为了我们心爱的京电

    京州电子科技大学遭遇废校危机,为了保护我们心爱的学校,N位魔法少女站了出来,她们能做的就是……成为偶像! 每个魔法少女都拥有一定的人气,他们中的每个人的人气计算方式如下: 假设某个魔法少女的学号为a, ...