0

i am trying to write a bash script for uploading instance id from EC2 instance to AWS SQS but bash does not allow me to substitute variable(${instanceId}) value inside single quotes. also i do have multiple double quotes inside, so this is little tricky. if any alternate please suggest.

#/bin/bash
instanceIdFile="/var/lib/cloud/data/instance-id"
instanceId=`cat $instanceIdFile`
echo $instanceId
aws sqs send-message --queue-url https://sqs.us-west-2.amazonaws.com/123412341234/requestQueue --message-body "example message body goes here1" --region us-west-2 --message-attributes '{"InstanceId":{ "DataType":"String","StringValue":"${instanceId}"}, "Status":{ "DataType":"String","StringValue":"Completed"} }'
  • 1
    You can change quoting types midway through a string. Just because the start and end are single-quoted doesn't mean the inside all has to be single-quoted. `'this is single-quoted'"this is double-quoted"'this is single-quoted again'` -- so you can use `$variable` references in the double-quoted part. – Charles Duffy Sep 28 '22 at 12:29
  • 1
    ...but for substituting data into JSON, this is all the wrong approach and you should be using `jq` instead. – Charles Duffy Sep 28 '22 at 12:29
  • 1
    BTW, `echo $instanceId` is itself buggy. _Always_ quote your expansions; `echo "$instanceId"` -- see [I just assigned a variable, but `echo $variable` shows something else](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) – Charles Duffy Sep 28 '22 at 12:31
  • 1
    (in your particular example, changing quoting types mid-string would look like `'..."StringValue":"'"${instanceId}"'"...'`) – Charles Duffy Sep 28 '22 at 12:33

0 Answers0