gpt4 book ai didi

shell - 将一个 shell 脚本的所有参数传递给另一个 shell 脚本

转载 作者:行者123 更新时间:2023-12-02 17:41:29 38 4
gpt4 key购买 nike

我有一个 shell 脚本,我向其中传递了一些参数。Test1.sh -a 1 -b 2 -c“一二三”

在 Test1.sh 中,我按以下方式调用另一个 shell 脚本。Test2.sh $*

我想将所有参数传递给 Test2,这些参数已传递给 Test1,并且格式也相同(带双引号等)。然而传递给 Test2 的参数是Test2.sh -a 1 -b 2 -c 一二三这对我不起作用。有没有办法解决这个问题,以便我可以像传递给 Test1 一样传递参数。

谢谢扎比

最佳答案

你需要说:

Test2.sh "$@"

引用Special Parameters :

@

Expands to the positional parameters, starting from one. 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. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).

手册说:

"$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable.

这解释了您观察到的结果。

关于shell - 将一个 shell 脚本的所有参数传递给另一个 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18492443/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com