shell 脚本中的 $@ 和 $* 有什么区别? [英] What is the difference between $@ and $* in shell scripts?

查看:39
本文介绍了shell 脚本中的 $@ 和 $* 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在shell脚本中,$@$*有什么区别?

In shell scripts, what is the difference between $@ and $*?

获取脚本参数的首选方法是哪一种?

Which one is the preferred way to get the script arguments?

不同的 shell 解释器在这方面是否存在差异?

Are there differences between the different shell interpreters about this?

推荐答案

来自 此处:

$@ 的行为与 $* 类似,只是在引用时,如果参数中有空格,则会正确分解.

$@ behaves like $* except that when quoted the arguments are broken up properly if there are spaces in them.

以这个脚本为例(取自链接的答案):

Take this script for example (taken from the linked answer):

for var in "$@"
do
    echo "$var"
done

给出这个:

$ sh test.sh 1 2 '3 4'
1
2
3 4

现在将 "$@" 改为 $*:

for var in $*
do
    echo "$var"
done

你会得到这个:

$ sh test.sh 1 2 '3 4'
1
2
3
4

(使用 Google 找到的答案)

(Answer found by using Google)

这篇关于shell 脚本中的 $@ 和 $* 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆