Home > front end >  String comparison in [[ ]] failing when target string contains quotes
String comparison in [[ ]] failing when target string contains quotes

Time:11-09

I am very confused as to why this while loop does not terminate, even though stack_status looks like it is equal to CREATE_COMPLETE. Did I forget something trivial?

stack_status=""
while [[ $stack_status != "CREATE_COMPLETE" ]]; 
do
   stack_status=$(aws cloudformation --region us-east-2 describe-stacks --stack-name ${stack_name} --query Stacks[0].StackStatus);   
   echo "Waiting for stack to complete"; 
   echo $stack_status;   
   sleep 5; 
done

Output

Waiting for stack to complete
"CREATE_COMPLETE"
Waiting for stack to complete
"CREATE_COMPLETE"
Waiting for stack to complete
"CREATE_COMPLETE"
Waiting for stack to complete
"CREATE_COMPLETE"

CodePudding user response:

If you want the double quotes to be considered literal data, put single quotes around them.

while [[ $stack_status != '"CREATE_COMPLETE"' ]]; do
  • Related