解析/将命令行参数传递给 bash 脚本 - “$@"和“$@"之间有什么区别?和“$*"? [英] Parsing/passing command line arguments to a bash script - what is the difference between "$@" and "$*"?

查看:27
本文介绍了解析/将命令行参数传递给 bash 脚本 - “$@"和“$@"之间有什么区别?和“$*"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

There are 3 variables in total, the first specifies 1 of 2 actions that the .jar is 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.

我目前使用的脚本是:

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

我对 bash 脚本知之甚少,但是在为我的困境寻找另一个答案(现已修复)时,我遇到了 "$@""$*"引用命令行参数时.做更多的搜索把我带到这个网站:How To Wiki: How to read command line arguments in a bash script,但我无法找到有关这些论点的任何可靠信息,而不必费力地完成大量超出我的脑子的高级 bash 编程.

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.

既然我一直在闲聊,我的问题就相对简单了:

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

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

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

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

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

推荐答案

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

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

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

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

这篇关于解析/将命令行参数传递给 bash 脚本 - “$@"和“$@"之间有什么区别?和“$*"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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