Store Bash script arguments $@ in a variable

若如初见. 提交于 2019-11-30 10:32:44

You can store it in an array:

args=("$@")

then you can access each of the arguments with index: "${args[index]}" and all arguments at once with "${args[@]}".

It depends what you want to do. Using $@ keeps the command-line arguments separated, if you want to join them together as a single string then use $* instead. For example:

args="$*"
java A $args

When "$*" is used then the command-line arguments are joined into one string using the first character of IFS as the "glue" between each argument. By default that is a space. The only issue with this is if any of the arguments themselves contain whitespace. If you know that spaces are going to be used, not tabs, then you can mess with IFS:

oldIFS="$IFS"
IFS=$'\t'
args="$*"
java A.py $args

IFS="$oldIFS"

Edit: since you wish to keep them separate, then use an array:

args=("$@")
java A "${args[@]}"

Using an "index" of @ has a similar effect to "$@" (you can also use * to join elements together, just like "$*").

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!