将命令行参数解析/传递给 bash 脚本 - “$@”和“$*”之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16649467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:28:17  来源:igfitidea点击:

Parsing/passing command line arguments to a bash script - what is the difference between "$@" and "$*"?

bashargumentscommand-line-argumentsargument-passingcommand-line-parsing

提问by Escribblings

I am using a bash script to call and execute a .jarfile from any location without having to constantly enter its explicit path.

我正在使用 bash 脚本.jar从任何位置调用和执行文件,而无需不断输入其显式路径。

The .jarrequires additional variable parameters to be specified at execution, and as these can be anything, they cannot be hard coded into the script.

.jar需要在执行时指定额外的变量参数,因为这些参数可以是任何东西,所以它们不能硬编码到脚本中。

There are 3 variables in total, the first specifies 1 of 2 actions that the .jaris to make, the second specifies a target file to enact this action on and the third specifies the name of a file that the action is to create.

总共有 3 个变量,第一个指定要执行的 2 个操作中的.jar一个,第二个指定要在其上执行此操作的目标文件,第三个指定该操作要创建的文件的名称。

The script I am currently using is:

我目前使用的脚本是:

#!/bin/bash  
java -jar "C:\path\to\file.jar" "" "" ""

I know very little about bash scripting, but while searching for another answer to my woes (now fixed) I came across "$@"and "$*"when referencing command line arguments. Doing more searching brought me to this site: How To Wiki: How to read command line arguments in a bash script, but I can't find any solid information about those arguments without having to wade through tons of advanced bash programming that is way above my head.

我不是很了解的bash脚本,但在寻找另一种答案我的困境(现已修复),我碰上了"$@""$*"引用命令行参数的时候。做更多的搜索让我来到这个网站:How To Wiki: How to read command line arguments in a bash script,但我无法找到任何关于这些参数的可靠信息,而不必涉足上面的大量高级 bash 编程我的头。

So now that I have rambled on forever, my question is relatively simple:

所以现在我已经漫无目的了,我的问题相对简单:

Can I replace "$1" "$2" "$3"with "$@"or "$*", and if so which is the better one to use?

我可以"$1" "$2" "$3""$@"或替换"$*",如果可以,哪个更好用?

Also what, if any, is the difference between those commands?

另外,如果有的话,这些命令之间的区别是什么?

回答by choroba

"$@"corresponds to "$1" "$2" "$3"etc. "$*"corresponds to "$1 $2 $3"which you do not seem to need.

"$@"对应"$1" "$2" "$3""$*"对应于"$1 $2 $3"您似乎不需要的。

Without quotes, there is no difference, they both correspond to $1 $2 $3etc.

没有引号,没有区别,它们都对应$1 $2 $3等。

回答by mogul

$*is all parameters as a single word, $@is all parameters as individual quoted string.

$*是所有参数作为一个词,$@是所有参数作为单独的带引号的字符串。

I usually ends up using "$@", seems to work the best for me.

我通常最终使用"$@", 似乎对我来说效果最好。

回答by dgatwood

$*contains a single string composed of all of the arguments separated by the first character of $IFS. Thus, if you set IFSto contain a character that cannot usefully be present in your arguments (e.g. a newline if you're passing filenames), then you can consider $*and "$@"to be functionally equivalent, though they work differently under the hood.

$*包含由所有参数组成的单个字符串,由 的第一个字符分隔$IFS。因此,如果您设置IFS为包含一个无法有效出现在您的参数中的字符(例如,如果您传递文件名,则为换行符),那么您可以考虑$*"$@"在功能上等效,尽管它们在后台的工作方式不同。

$@is essentially an array of all of the arguments. When passed in double quotation marks ("$@"), the shell individuallywraps each value in the array with double quotes (making it an odd exception to the normal rule of double quotes wrapping the entire expansion of a variable).

$@本质上是所有参数的数组。当传入双引号 ( "$@") 时,shell单独用双引号将数组中的每个值包装起来(使其成为双引号包装整个变量扩展的正常规则的奇怪例外)。

IIRC, the behavior of a bare $@without double quotes is not fully specified in the POSIX spec, so it should be considered undefined behavior. Thus, you should never use a bare $@without double quotes.

IIRC,$@没有双引号的裸的行为在 POSIX 规范中没有完全指定,所以它应该被认为是未定义的行为。因此,你永远$@不应该使用没有双引号的裸。

BTW, if you care about System V prior to version 3, there were some ancient quirksin how they handled $@.These are largely of historical interest at this point, but they'll give you some appreciation of how the standard evolved as people ran into problems. :-)

顺便说一句,如果您关心版本 3 之前的 System V,那么它们的处理方式存在一些古老的怪癖$@.这些在这一点上主要具有历史意义,但它们会让您对标准如何随着人们遇到问题而演变有所了解. :-)