访问bash命令行参数$ @ vs $ *

本文翻译自:Accessing bash command line args $@ vs $*

In many SO questions and bash tutorials I see that I can access command line args in bash scripts in two ways: 在许多SO问题和bash教程中,我看到我可以通过两种方式访问​​bash脚本中的命令行args:

$ ~ >cat testargs.sh 
#!/bin/bash

echo "you passed me" $*
echo "you passed me" $@

Which results in: 结果是:

$ ~> bash testargs.sh arg1 arg2
you passed me arg1 arg2
you passed me arg1 arg2

What is the difference between $* and $@ ? $*$@什么区别?
When should one use the former and when shall one use the latter? 什么时候应该使用前者,什么时候应该使用后者?


#1楼

参考:https://stackoom.com/question/PFyb/访问bash命令行参数-vs


#2楼

$@ is 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. 这意味着,除其他事项外,参数列表中的每个参数都被视为一个单独的单词。

Of course, "$@" should be quoted. 当然,应引用“ $ @”。

http://tldp.org/LDP/abs/html/internalvariables.html#ARGLIST http://tldp.org/LDP/abs/html/internalvariables.html#ARGLIST


#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 variable. 当在双引号内进行扩展时,它将扩展为一个单词,每个参数的值由IFS特殊变量的第一个字符分隔。 That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. 也就是说,“ $ *”等效于“ $ 1c $ 2c ...”,其中c是IFS变量值的第一个字符。 If IFS is unset, the parameters are separated by spaces. 如果未设置IFS,则参数之间用空格分隔。 If IFS is null, the parameters are joined without intervening separators. 如果IFS为null,则连接参数时不插入分隔符。

$@ $ @

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. 也就是说,“ $ @”等效于“ $ 1”“ $ 2” ...如果在单词中出现双引号扩展名,则第一个参数的扩展名将与原始单词的开头部分连接在一起,并且扩展名最后一个参数中的表示与原始单词的最后一部分结合在一起。 When there are no positional parameters, "$@" and $@ expand to nothing (ie, they are removed). 当没有位置参数时,“ $ @”和$ @扩展为空(即,它们被删除)。

Source: Bash man 资料来源: Bash man


#4楼

The difference appears when the special parameters are quoted. 引用特殊参数时会出现差异。 Let me illustrate the differences: 让我来说明差异:

$ set -- "arg  1" "arg  2" "arg  3"

$ for word in $*; do echo "$word"; done
arg
1
arg
2
arg
3

$ for word in $@; do echo "$word"; done
arg
1
arg
2
arg
3

$ for word in "$*"; do echo "$word"; done
arg  1 arg  2 arg  3

$ for word in "$@"; do echo "$word"; done
arg  1
arg  2
arg  3

one further example on the importance of quoting: note there are 2 spaces between "arg" and the number, but if I fail to quote $word: 关于引号重要性的另一个示例:请注意,“ arg”和数字之间有2个空格,但是如果我不能引述$ word:

$ for word in "$@"; do echo $word; done
arg 1
arg 2
arg 3

and in bash, "$@" is the "default" list to iterate over: 在bash中, "$@"是要迭代的“默认”列表:

$ for word; do echo "$word"; done
arg  1
arg  2
arg  3

#5楼

A nice handy overview table from the Bash Hackers Wiki : Bash Hackers Wiki上的一个不错的概览表:

$ *与$ @表

where c in the third row is the first character of $IFS , the Internal Field Separator, a shell variable. 其中第三行中的c$IFS的第一个字符,即内部字段分隔符(一个shell变量)。

If the arguments are to be stored in a script variable and the arguments are expected to contain spaces, I wholeheartedly recommend employing a "$*" trick with the internal field separator $IFS set to tab . 如果参数要存储在脚本变量中 ,并且参数应包含空格,我会全力建议使用内部字段分隔符$IFS设置为tab"$*"技巧


#6楼

This example let may highlight the differ between "at" and "asterix" while we using them. 这个示例让我们可以突出显示在使用它们时“ at”和“ asterix”之间的区别。 I declared two arrays "fruits" and "vegetables" 我宣布了两个数组“水果”和“蔬菜”

fruits=(apple pear plumm peach melon)            
vegetables=(carrot tomato cucumber potatoe onion)

printf "Fruits:\t%s\n" "${fruits[*]}"            
printf "Fruits:\t%s\n" "${fruits[@]}"            
echo + --------------------------------------------- +      
printf "Vegetables:\t%s\n" "${vegetables[*]}"    
printf "Vegetables:\t%s\n" "${vegetables[@]}"    

See the following result the code above: 请参阅以下代码,查看以下结果:

Fruits: apple pear plumm peach melon
Fruits: apple
Fruits: pear
Fruits: plumm
Fruits: peach
Fruits: melon
+ --------------------------------------------- +
Vegetables: carrot tomato cucumber potatoe onion
Vegetables: carrot
Vegetables: tomato
Vegetables: cucumber
Vegetables: potatoe
Vegetables: onion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值