bash的几个特殊参数和位置参量
http://blog.csdn.net/jiankun_wang/article/details/4336285
一、$*和$@
首先介绍两个极其相似、很难区分的特殊参数$*和$@,先看如下输出:
wangjk@wangjiankun:~$ cat test.sh
1 #!/bin/bash
2
3 echo "/$@ is $@"
4 echo "/$* is $*"
5
wangjk@wangjiankun:~$ ./test.sh a b c d
$@ is a b c d
$* is a b c d
wangjk@wangjiankun:~$奥,原来$@和$*都是代表所有的输入参数,那它俩有区别吗?再看看下面的输出:
wangjk@wangjiankun:~$ cat test1.sh
1 #!/bin/bash
2
3 set 'apple pie' pears peaches
4
5 echo
6 echo "/$*"
7 for i in $*
8 do
9 echo $i
10 done
11
12 echo
13 echo "/"/$*/""
14 for i in "$*"
15 do
16 echo $i
17 done
18
19 echo
20 echo "/$@"
21 for i in $@
22 do
23 echo $i
24 done
25
26 echo
27 echo "/"/$@/""
28 for i in "$@"
29 do
30 echo $i
31 done
wangjk@wangjiankun:~$ ./test1.sh$*
apple
pie
pears
peaches"$*"
apple pie pears peaches$@
apple
pie
pears
peaches"$@"
apple pie
pears
peaches
wangjk@wangjiankun:~$上面的输出显示,当$@和$*都不加双引号时,输出结果相同,并且它们都将空格作为字符串的分割符;在加上双引号时,"$*"将所有的输入参数视作是一个字符串,等于“$1 $2 $3”,而"$@"将每一个输入参数视作一个字符串,等于“$1” “$2” “$3”。那么当$@和$*都不加双引号时,它们的输出结果真的永远一样吗?看下面的输出结果:
wangjk@wangjiankun:~$ cat test.sh
1 #!/bin/bash
2
3 echo "/$@ is $@"
4 echo "/$* is $*"
5 echo "/"/$@/" is "$@""
6 echo "/"/$*/" is "$*""
7
8 echo
9 IFS=""
10 echo "/$@ is $@"
11 echo "/$* is $*"
12 echo "/"/$@/" is "$@""
13 echo "/"/$*/" is "$*""
wangjk@wangjiankun:~$ ./test.sh a b c d
$@ is a b c d
$* is a b c d
"$@" is a b c d
"$*" is a b c d$@ is a b c d
$* is abcd
"$@" is a b c d
"$*" is a b c d
wangjk@wangjiankun:~$可以看出:脚本输出的上下两部分的不同(红色的abce)是由于在脚本中定义了一个变量IFS的原因,因为除此之外,脚本的上下两部分是完全相同的,看来$@和$*还是有不同的。事实上,$@和$*在分割符的使用上也存在着不同:$@使用的分割符是空格,而$*使用的分割符是变量IFS的第一个字符(注意:IFS变量不是一个字符,而是一个字符序列)。这样就容易解释上面的输出了。
总之,$@和$*有两点不同:
1、分割符的使用不同;
2、对于双引号的解释不同。
下面摘引两本书上对于二者的解释。
一本来自中文书籍,如下:
$* 其值为所有的位置参量
$@ 除了被双引号引用的情况,含义与$*相同
"$*" 其值为"$1 $2 $3"
"$@" "$1" "$2" "$3"
一本来自英文书籍,如下:
$* Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special varible.
$@ Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word.
bash的man手册有如下解释:
* Expands to the positional parameters, starting from one. When
the expansion occurs within double quotes, it expands to a sin-
gle word with the value of each parameter separated by the first
character of the IFS special variable. That is, "$*" is equiva-
lent to "$1c$2c...", where c is the first character of the value
of the IFS variable. If IFS is unset, the parameters are sepa-
rated by spaces. If IFS is null, the parameters are joined
without intervening separators.
@ Expands to the positional parameters, starting from one. When
the expansion occurs within double quotes, each parameter
expands to a separate word. That is, "$@" is equivalent to "$1"
"$2" ... If the double-quoted expansion occurs within a word,
the expansion of the first parameter is joined with the
beginning part of the original word, and the expansion of the
last parameter is joined with the last part of the original
word. When there are no positional parameters, "$@" and $@
expand to nothing (i.e., they are removed).
二、简单介绍其他的几个容易理解的特殊参量和位置参数
其实,还有很多特殊参数,我在此不一一介绍,下面只介绍几个常用的。
1、$# 以十进制数的形式显示位置参数的个数
2、$0 当前脚本的名称,注意,如果使用命令./scriptname.sh运行脚本,则$0 = = ./scriptname.sh
2、$1、$2、$3、…、${10}、${11}、${12}、…
分别是第一个、第二个、第三个、…、第十个、第十一个、第十二个参数,注意,从第十个开始要加大括号。
最新文章
- mseed2sac的安装和使用
- 一款效果精致的 jQuery 多层滑出菜单插件
- django中抽象基类的Foreignkey的定义
- android常见问题
- UIImage 和 iOS 图片压缩UIImage / UIImageVIew
- Inside Flask - app.py - 2
- AngularJs学习笔记--directive
- 请问什么是UTF字符串?
- POJ2773 - Happy 2006(欧拉函数)
- Qt编程之Qt样例表(QSS)
- 线性表之顺序表(C语言实现)
- DebugView使用技巧
- hdu4521(线段树+dp)
- 关于Verilog HDL的一些技巧、易错、易忘点(不定期更新)
- fio2.1.10--README
- The 16th tip of DB Query Analyzer
- GetMemory那一题的理解
- jupyter nootbook本地使用指南
- PAT 1029 旧键盘(20)(代码)
- 解决SQL Server 2008提示评估期已过
热门文章
- 视频播放插件Video.js
- [LOJ#522]「LibreOJ β Round #3」绯色 IOI(危机)
- [BZOJ3817]Sum
- Java 微信公众号开发--- 接入微信
- 【BZOJ1901】Dynamic Rankings(树套树,树状数组,主席树)
- Codeforces 842C Ilya And The Tree 树上gcd
- Chrome 浏览器如何完美实现滚动截图技巧
- POJ - 2135最小费用流
- Codeforces 889C Maximum Element(DP + 计数)
- OS | 冯诺伊曼体系和哈佛体系