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. Android与html5交互 -- WebView使用(一)

    Android中使用WebView可加载html5,具体步骤如下: (前提:本地Html5存放到assets文件夹下) 一:使用WebView加载Html5,简单显示 1:清单文件中添加访问权限:an ...

  2. [水煮 ASP.NET Web API2 方法论](1-6)Model Validation

    问题 想要 ASP.NET Web API 执行模型验证,同时可以和 ASP.NET MVC 共享一些验证逻辑. 解决方案 ASP.NET Web API 与 ASP.NET MVC 支持一样的验证机 ...

  3. cocos2djs ctor init onEnter的区别

    cocos2d-html5 onEnter init ctor构造函数 ---js特有特性(和c++有点不一样 ctor 构造函数, new 一个对象的时候调用-----coco2d-js , 默认c ...

  4. gvim 编辑器配置

    "关才兼容模式 set nocompatible "模仿快捷键,如:ctrt+A 全选.Ctrl+C复制. Ctrl+V 粘贴等 source $VIMRUNTIME/vimrc_ ...

  5. spark sql遇到的问题

    项目背景:计算平台架构hadoop spark   spark on yarn,支持多种sql引擎如hive.spark sql.impala等. 问题描述:SPARK SQL在做insert ove ...

  6. 线段树+扫描线【bzoj1645】[USACO07OPEN]城市的地平线City Horizon

    Description 约翰带着奶牛去都市观光.在落日的余晖里,他们看到了一幢接一幢的摩天高楼的轮廓在地平线 上形成美丽的图案.以地平线为 X 轴,每幢高楼的轮廓是一个位于地平线上的矩形,彼此间可能有 ...

  7. BZOJ 4873 寿司餐厅(最大权闭合图 网络流)

    寿司餐厅 时间限制: 1 Sec  内存限制: 512 MB提交: 6  解决: 3[提交][状态][讨论版] 题目描述 Kiana 最近喜欢到一家非常美味的寿司餐厅用餐.每天晚上,这家餐厅都会按顺序 ...

  8. Linux命令之find(二)

    接上一篇Linux命令之find(一) (1).实例 1.列出当前目录下及子目录下所有的.txt文件 [xf@xuexi ~]$ ls 1.txt 3.txt b.txt 公共 视频 文档 音乐 2. ...

  9. Cobol online program 传指针

  10. 安卓 内存泄漏 MemoryAnalyzer

    韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com 需要 获取 root 权限 步骤: 1,使用eclipse 自带的 DDMS 工具分析各线程的内 ...