免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1814 | 回复: 4
打印 上一主题 下一主题

bash shell : 怎么才能用程序比较出变量$@和$*的差别呀。我比较不出来。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-03 20:28 |只看该作者 |倒序浏览
真的弄不明白这两个变量有什么不同,希望高人能给点比较详细的解释。谢谢。

论坛徽章:
0
2 [报告]
发表于 2009-09-06 11:40 |只看该作者
$*
All of the positional parameters, seen as a single word
"$*" must be quoted.
$@
Same as $*, but each parameter is a quoted string, that is, the parameters are passed on intact, without
interpretation or expansion. This means, among other things, that each parameter in the argument list
is seen as a separate word.
-------
$*  ===> "$1 $2...."
$@ ===> "$1" "$2" ....

论坛徽章:
0
3 [报告]
发表于 2009-09-06 14:54 |只看该作者

回复 #2 Henk2009 的帖子

我的e1.sh如下
#!/bin/sh
echo $*
或者
#!/bin/sh
echo $@
或者
#!/bin/sh
echo "$*"
或者
#!/bin/sh
echo "$@"

运行脚本:./e1.sh a b c "d e" 后的结果都是下面这样子
a b c d e

论坛徽章:
0
4 [报告]
发表于 2009-09-06 21:19 |只看该作者
=======test1.sh=====
#!/bin/sh
./test2.sh "$*"
./test2.sh "$@"


======test2.sh==========
#!/bin/sh

for((i=0; i<$#;i++))
do
    echo $1
   shift
done



运行   ./test1.sh 1 2 3 4 5就可以看出来了

论坛徽章:
0
5 [报告]
发表于 2009-09-08 14:45 |只看该作者

非常感谢楼上的兄弟。终于搞明白了

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP