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}、…       

                  分别是第一个、第二个、第三个、…、第十个、第十一个、第十二个参数,注意,从第十个开始要加大括号。

原文地址:https://www.cnblogs.com/baiyw/p/3504787.html