当目标字符串包含引号时 [[ ]] 中的字符串比较失败

String comparison in [[ ]] failing when target string contains quotes

我很困惑为什么这个 while 循环没有终止,即使 stack_status 看起来等于 CREATE_COMPLETE。我是不是忘记了什么小事?

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

输出

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"

如果您希望双引号被视为文字数据,请在其两边加上单引号。

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