1

In the script below, I can't seem to make $var1 expand in the second statement. I've tried $var1, ${var1}, echo $var1 and '$var1'. It is inside a few sets of quotes and parentheses which I guess is what is causing the problem. Any ideas?

#!/bin/bash

# Get the AutoScalingGroupName for the NameNode ASG
var1=$(aws cloudformation list-stack-resources --stack-name abc123 | jq '.StackResourceSummaries[] | select(.ResourceType=="AWS::AutoScaling::AutoScalingGroup")' | jq '.PhysicalResourceId' | tr -d '"' | grep nn); echo $var1

var2=$(aws autoscaling describe-auto-scaling-instances | jq -r '.AutoScalingInstances[] | select(.AutoScalingGroupName == "$var1") | select(.AvailabilityZone == "us-east-1a") .InstanceId'); echo $var2
5

Variables in single quotes are not expanded. Try this...

var2=$(aws autoscaling describe-auto-scaling-instances | jq -r '.AutoScalingInstances[] | select(.AutoScalingGroupName == "'"$var1"'") | select(.AvailabilityZone == "us-east-1a") .InstanceId'); echo $var2
0

It is wrapped in single-quotes, so it cannot be expanded.

jq -r '.AutoScalingInstances[] | select(.AutoScalingGroupName == "$var1") ... .InstanceId'

The key part is the jq command that you are actually only passing a single argument to (notice the long red part of the command). So your pipes are also not taking effect. You can selectively wrap things in quotes that truly need to be.

  • 1
    Micah, thanks for your feedback but I'm not exactly sure what you mean. Once I made the change suggested above, it produced the output I was expecting. – Sco Jul 30 '15 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.