首页 > 解决方案 > shell中字符串中的字符串

问题描述

下面是我的代码示例

command mycommand /path/location arg1="my valu1" arg2="my value2"

当我通过对参数值进行硬编码来执行此命令时,它会起作用。

但是当我使用以下代码格式时:

for i in "$@" do
str+="$i " 
done
command mycommand /path/location $str

where $str="arg1="my valu1" arg2="my value2""

它不接受“my value1”作为单个字符串。它将“my”作为一个字符串,将“value2”作为一个单独的字符串。

现在如果我使用

command mycommand /path/location "$str"

然后 $str="arg1="my valu1" arg2="my value2"" 作为一个完整的字符串。但我希望命令应该通过程序方式以以下格式执行。

command mycommand /path/location arg1="my valu1" arg2="my value2"

这个怎么做?

标签: shellunix

解决方案


Don't try to append your arguments into a string just pass along "$@" to your actual command inside the script:

command mycommand /path/location "$@"

推荐阅读