当前位置: > shell编程 >

实例详解shell中$和$@的区别

时间:2014-11-23 14:06来源:linux.it.net.cn 作者:IT网


shell编程语言中$与$@的区别,分享一个完整的示例代码,深入探讨下二者的区别


shell中$和$@的区别

例子:
 

复制代码代码示例:
#!/usr/bin/bash
#site: www.jbxue.com
#
E_BADARGS=65  
   
if [ ! -n "$1" ]  
then  
  echo "Usage: `basename $0` argument1 argument2 etc."  
  exit $E_BADARGS  
fi  
echo  
index=1  
   
for arg in "$*"  
do  
  echo "Arg #$index = $arg"  
  let "index+=1"  
done  
echo "Entire arg list as single word."  
echo  
index=1  
   
for arg in "$@"  
do  
  echo "Arg #$index = $arg"  
  let "index+=1"  
done  
echo "Entire arg list as seperated words."  
echo  
index=1  
   
for arg in $*  
do  
  echo "Arg #$index = $arg"  
  let "index+=1"  
done  
echo "Entire arg list as seperated words."  
echo  
index=1  
   
for arg in $@  
do  
  echo "Arg #$index = $arg"  
  let "index+=1"  
done  
echo "Entire arg list as seperated words."  
exit 0  
 

执行结果:
 

复制代码代码示例:
$ ./test.sh bi yutong  
  
Arg #1 = bi yutong  
Entire arg list as single word.  
  
Arg #1 = bi  
Arg #2 = yutong  
Entire arg list as seperated words.  
  
Arg #1 = bi  
Arg #2 = yutong  
Entire arg list as seperated words.  
  
Arg #1 = bi  
Arg #2 = yutong  
Entire arg list as seperated words

以上通过示例,介绍了shell中$和$@的区别,希望对大家有所帮助。

(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容