【Shell】linux中shell变量$#,$@,$0,$1,$2的含义解释 && set 关键字使用
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 QQThe complete list is 24249The complete list isThe complete list is 0The complete list is 123456 QQThe complete list is 123456The complete list is QQThe complete list is 2The complete list is params.shThe complete list is 123456The complete list is QQ |
# ! /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
随机推荐
- 详解WordPress中简码格式标签编写的基本方法
WordPress 简码是一种类似于论坛标签的东西,格式类似于把尖括号换成中括号的 Html 标签.简码很多人叫做短代码,但官方的翻译应该是简码,在这里纠正一下. 简码的开发的逻辑比较简单,主要就是添 ...
- [BZOJ4566][Haoi2016]找相同字符 后缀自动机+dp
4566: [Haoi2016]找相同字符 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1212 Solved: 694[Submit][Stat ...
- centos 7 开机启动配置
centos 7 开机启动 1 开机启动配置文件位于/usr/lib/systemd/system/ 2 nginx的配置[Unit]Description=nginx - high performa ...
- cocos2d-x addImageAsync()异步加载资源成功之后的场景跳转问题
http://blog.csdn.net/w20175357/article/details/23546985 1.先说说addImageAsync()异步加载图片的问题 做游戏的时候现在资源的比较大 ...
- 获取或设置config节点值
ExeConfigurationFileMap 这个类提供了修改.获取指定 config 的功能:新建一个 ExeConfigurationFileMap 的实例 ecf :并设置 ExeConfig ...
- 深度学习应用系列(二) | 如何使用keras进行迁移学习,以训练和识别自己的图片集
本文的keras后台为tensorflow,介绍如何利用预编译的模型进行迁移学习,以训练和识别自己的图片集. 官网 https://keras.io/applications/ 已经介绍了各个基于Im ...
- 洛谷——P2799 国王的魔镜
P2799 国王的魔镜 题目描述 国王有一个魔镜,可以把任何接触镜面的东西变成原来的两倍——只是,因为是镜子嘛,增加的那部分是反的.比如一条项链,我们用AB来表示,不同的字母表示不同颜色的珍珠.如果把 ...
- oracle substr
SUBSTR( string, start_position [, length ] ) Parameters or Arguments string The source string. start ...
- [BZOJ4698][SDOI2008]Sandy的卡片(后缀自动机)
差分之后就是求多串LCS. 对其中一个串建SAM,然后把其它串放在上面跑. 对SAM上的每个状态都用f[x]记录这个状态与当前串的最长匹配长度,res[x]是对每次的f[x]取最小值.答案就是res[ ...
- [BZOJ5011][JXOI2017]颜色
5011: [Jx2017]颜色 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 84 Solved: 46[Submit][Status][Disc ...