简体   繁体   English

shell命令末尾的“$ *”是什么意思

[英]What does “$*” at the end of a shell command mean

In a sample shell script, the command was - 在示例shell脚本中,命令是 -

. <sourced_file.sh> $*

What does the $* mean? $*是什么意思? Thanks. 谢谢。

$* expands to all of the arguments that were given to the script in which it appears, or to the current shell function if it appears inside a function. $*扩展到给出它出现的脚本的所有参数,或者扩展到当前shell函数(如果它出现在函数内)。

It's usually incorrect usage though, because it breaks arguments that contain spaces into multiple arguments. 但它通常是不正确的用法,因为它会将包含空格的参数分解为多个参数。 More correct is "$@" which preserves the original arguments even if they have spaces in them. 更正确的是"$@" ,即使它们中包含空格,也会保留原始参数。

$* is a variable holding all positional parameters starting from 1 (the arguments to the current shell script) $*是一个变量,包含从1开始的所有位置参数(当前shell脚本的参数)

man 1 bash : 男子1猛击

The shell treats several parameters specially. shell专门处理几个参数。 These parameters may only be referenced; 这些参数只能被引用; assignment to them is not allowed. 不允许分配给他们。

* *

Expands to the positional parameters, starting from one. 从1开始扩展到位置参数。 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. 从1开始扩展到位置参数。 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). 当没有位置参数时,“$ @”和$ @扩展为空(即,它们被删除)。

Usually you want to use "$@" though: 通常你想使用"$@"

"$*" is equivalent to "$1 $2 ..." whereas “$ *”相当于“$ 1 $ 2 ......”而
"$@" is equivalent to "$1" "$2" ... “$ @”相当于“$ 1”“$ 2”......

$* is the alias for all the arguments given to the current script. $*是给当前脚本的所有参数的别名。

For example, if you launch : 例如,如果您启动:

./test_script.sh arg1 arg2 arg3

If you do echo $* in test_script.sh you will display : arg1 arg2 arg3 如果你在arg1 arg2 arg3 echo $* ,你将显示: arg1 arg2 arg3

Google on bash $* gives you immediately the advanced bash scripting guide which gives you the answer. Google on bash $*立即为您提供高级bash脚本编写指南 ,为您提供答案。 You often should prefer "$@" 你经常应该选择"$@"

$* are the arguments $ *是参数

So if you script "foo.sh" is 所以,如果你脚本“foo.sh”是

#!/bin/sh
gethostip $*

And you call foo.sh -d localhost 你调用foo.sh -d localhost

it will be the same as 它会是一样的

gethostip -d localhost

Minbd though, $* will not play nice with arguments having spaces. 但是,Minbd,$ *对于有空格的参数不会很好。

It's “all the arguments”: 这是“所有论点”:

$ cat >test<<EOF
> echo \$*
> EOF
$ bash test foo bar
foo bar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM