0

I am running the below awscli command in a bash script through teamcity step

 #!/bin/bash 
  repo=%env.RepoName%
  echo "repo is ${repo}"
  tags=%env.Tags%
  echo "tags are ${tags}"
  aws ecr create-repository --repository-name ${repo} --tags '${tags}'

where %env.RepoName% and %env.Tags% are teamcity variables with values sample-repo and [{"Key":"env","Value":"dev"},{"Key":"dept","Value":"finance"}] respectively. However when the aws cli command runs it errors out with the below error

Expecting property name enclosed in double quotes: line 1 column 3 (char 2)

However if i pass in the value of %env.Tags% as '[{"Key":"env","Value":"dev"},{"Key":"dept","Value":"finance"}]' with the single quotes and exclude the '' from the tags property in the aws cli command it runs without issues.

What is that I am missing and is there a recommended way to fix this?

fledgling
  • 991
  • 4
  • 25
  • 48
  • 2
    Single quotes suppress expansion, so you're literally sending `${tags}` and not its contents. – Benjamin W. Sep 04 '20 at 03:21
  • 1
    Does this answer your question? [Difference between single and double quotes in Bash](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash) – Aserre Sep 04 '20 at 05:59
  • @Aserre and Benjamin Thank you. The above link explains why this is not working. But I am still not sure how I would fix my particular issue. Do you have thoughts, please? – fledgling Sep 04 '20 at 08:07
  • In other words, Is there a way I can exclude `' '` when passing the input and still make the aws-cli command work within the bash script? – fledgling Sep 04 '20 at 08:21

1 Answers1

0

One way is to keep the double quotes by escaping them with a \. You may need to write sed to do this:

tags="[{\"Key\":\"env\",\"Value\":\"dev\"},\"Key\":\"dept\",\"Value\":\"finance\"}]"

Once it is done, verify the command using echo to check if the quotes are preserved.

echo aws ecr create-repository --repository-name ${repo} --tags "${tags}"                         
aws ecr create-repository --repository-name sample-repo --tags [{"Key":"env","Value":"dev"},{"Key":"dept","Value":"finance"}]

Another way is using Short hand syntax. If you can get the tag keys/values separately, this syntax seems cleaner.

aws ecr create-repository --repository-name ${repo} --tags Key=env,Value=dev Key=dept,Value=finance
ryandam
  • 684
  • 5
  • 13