更改命令行参数 - 庆典 [英] Change a command line argument - bash
问题描述
有没有办法改变一个bash脚本的命令行参数。比方说,一个bash脚本调用方式如下:
Is there a way to change the command line arguments in a bash script. Say for example, a bash script is invoked the following way:
./foo arg1 arg2
有没有办法在脚本中改变Arg1的价值?再说了,像
Is there a way to change the value of arg1 within the script? Say, something like
$1="chintz"
谢谢,结果
斯利拉姆
Thanks,
Sriram
推荐答案
您必须重新设置所有参数。为了例如,改变 $ 3
:
You have to reset all arguments. To change e.g. $3
:
$ set -- "${@:1:2}" "new" "${@:4}"
编辑:
基本上你设置的所有的参数它们的当前值,除了要改变的人(S)。
Basically you set all arguments to their current values, except for the one(s) that you want to change.
$ {@:1:2}
符号被扩展为两个(在符号因此 2
)的位置参数偏离 1开始
(即 $ 1
)。正是在这种情况下,为的速记$ 1$ 2
,但是当你想如更换是更为有用$ {17}
。
The "${@:1:2}"
notation is expanded to the two (hence the 2
in the notation) positional arguments starting from offset 1
(i.e. $1
). It is a shorthand for "$1" "$2"
in this case, but it is much more useful when you want to replace e.g. "${17}"
.
这篇关于更改命令行参数 - 庆典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!