简体   繁体   中英

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.

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

#!/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.

++ 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

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?

thanks Art

The single quotes are an artifact of using xtrace and aren't actually part of the argument. 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 --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})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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