gpt4 book ai didi

linux - 如何使用存储在单个变量中的引号传递命令行参数?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:40:59 24 4
gpt4 key购买 nike

我想从 shell 脚本调用外部应用程序,但是这个 shell 脚本在单个变量中获取参数(从其他脚本)。一切正常,直到我不必为单个参数使用双引号,而是用空格分隔单词。

这是我的问题的简化示例(sh_param 只打印所有传递的参数):

#!/bin/sh

pass() {
echo "Result with \$@"
./sh_param $@
echo "Result with \"\$@\""
./sh_param "$@"
echo "Result with \$*"
./sh_param $*
echo "Result with \"\$*\""
./sh_param "$*"
}

pass '"single param" separate params'

和结果(sh_param 只打印所有传递的参数):

Result with $@
Param: "single
Param: param"
Param: separate
Param: params
Result with "$@"
Param: "single param" separate params
Result with $*
Param: "single
Param: param"
Param: separate
Param: params
Result with "$*"
Param: "single param" separate params

我想要:

Param: single param
Param: separate
Param: params

最佳答案

脚本

pass() {
echo 'Result with "$@"'
sh_param "$@"
}

sh_param() {
for i in "$@"
do
echo Param: $i
done
}

pass "single param" separate param

结果

Result with "$@"
Param: single param
Param: separate
Param: param

关于linux - 如何使用存储在单个变量中的引号传递命令行参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10449233/

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