shell 中字符串中的字符串 [英] A string within a string in shell

查看:36
本文介绍了shell 中字符串中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码示例

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

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

when I am executing this command by hard-coding the argument value it works.

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

But when I am using below code format:

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

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

它不接受my value1"作为单个字符串.它将my"作为一个字符串,将value2"作为一个单独的字符串.

It is not accepting "my value1" as a single string. It is taking "my" as one string and "value2" as a separate string.

现在如果我使用

command mycommand /path/location "$str"

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

then $str="arg1="my valu1" arg2="my value2"" as only one complete string. But I want command should execute in below format through program way.

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

如何做到这一点?

推荐答案

不要尝试将参数附加到字符串中,只需将 "$@" 传递给脚本中的实际命令即可:

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

command mycommand /path/location "$@"

这篇关于shell 中字符串中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆