Shell/Bash - 字符串之间的相等性检查 returns 1 匹配

Shell/Bash - equality check between strings returns 1 on match

我正在尝试将 cli 命令 "successful" 的输出与简单的 test = $? 进行比较,但得到 1 即使值匹配。我在硬编码示例上做了同样的事情并且工作正常。

存储cli输出的变量是否应该在不同的包装中?

#ri status:
ri_status=`aws dms describe-connections --filter Name=replication-instance-arn,Values=$ri_arn --query=Connections[0].Status --region us-east-1`

#returns "successful"
echo $ri_status

#this returns 0
test "successful" = "successful"
echo $?

#this returns 1
test $ri_status = "successful"
echo $?

已通过 '

修复

http://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Quoting

3.1.2.2 Single Quotes

Enclosing characters in single quotes (‘'’) preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

test $ri_status = '"successful"'