简体   繁体   English

为什么bash在命令的转义双引号中添加单引号

[英]why is bash adding single quotes to escaped double quotes for command

I am having trouble executing a command as part of a script on EC2 AWS-AMI Linux. 我在EC2 AWS-AMI Linux上将命令作为脚本的一部分执行时遇到麻烦。

As part of the command I must have some vars passed to the CLI including quotes such as --subnet-ids "subnet-c123456" "subnet-b123456" "subnet-a123456" so I have created the code block as 作为命令的一部分,我必须将一些var传递给CLI,包括--subnet-ids "subnet-c123456" "subnet-b123456" "subnet-a123456"因此我将代码块创建为

#!/bin/sh
set -x
......
MySubnetA="subnet-a123456"
MySubnetB="subnet-b123456"
MySubnetC="subnet-c123456"
$(aws --profile myProfile --region myRegion rds create-db-subnet-group --db-subnet-group-name ${!1} --db-subnet-group-description ${!1} --subnet-ids \"${MySubnetA}\" \"${MySubnetB}\" \"${MySubnetC}\"  --tags Key=Name,Value=${!1})

When I run the script the output shows the command with single quotes around the escaped double quotes, this causes the CLI to fail. 当我运行脚本时,输出将显示命令,并在转义的双引号周围加上单引号,这将导致CLI失败。

++ aws --profile myProfile --region us-west-1 rds create-db-subnet-group --db-subnet-group-name myDBgroup --db-subnet-group-description myDBgroup --subnet-ids '"subnet-a123456"' '"subnet-b123456"' '"subnet-c123456"' --tags Key=Name,Value=myDBgroup

How do I pass the command so double quotes are not enclosed with the single quotes. 如何传递命令,使双引号不包含在单引号中。

UPDATE UPDATE

As per comment below, if I echo the command string, then copy/paste run in terminal then the command executes as expected. 根据下面的评论,如果我回显命令字符串,然后在终端中运行复制/粘贴操作,则命令将按预期执行。 But when run inside script it fails. 但是在脚本内部运行时会失败。 Could the bash script be adding extra characters to the command causing it fail? bash脚本能否在命令中添加额外的字符,导致其失败?

thanks Art 感谢艺术

The single quotes are an artifact of using xtrace and aren't actually part of the argument. 单引号是使用xtrace的人工产物,实际上并不是参数的一部分。 They simply indicate that the double quotes are actually part of the argument and don't merely indicate that the argument is a single word. 它们只是表明双引号实际上是参数的一部分,而不仅仅是表明参数是一个单词。

There is no need to use escaped quotes around your variables inside the aws command, use it as: aws命令中无需在变量周围使用转义引号,可将其用作:

$(aws --profile myProfile --region myRegion rds create-db-subnet-group --db-subnet-group-name ${!1} --db-subnet-group-description ${!1} --subnet-ids ${MySubnetA} ${MySubnetB} ${MySubnetC} --tags Key=Name,Value=${!1})

Or else quote them without escaping: 否则请引用它们而不能转义:

$(aws --profile myProfile --region myRegion rds create-db-subnet-group --db-subnet-group-name ${!1} --db-subnet-group-description ${!1} --subnet-ids "$MySubnetA" "$MySubnetB" "$MySubnetC" --tags Key=Name,Value=${!1})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM