首页 > 解决方案 > IF 语句总是计算为真

问题描述

我有以下片段,其中函数testechoes falseif我在带有 shell 替换的语句中使用回显值:

#!/usr/bin/env bash

test () {
    echo "false"
}

if [[ "$(test)" -eq "true" ]]
then
    echo hello world
fi

我希望上面打印hello world,因为我认为这最终会说[[ "false" -eq "true" ]]

但是,当我运行脚本时,它会回显hello world

标签: bash

解决方案


尝试这个

#!/usr/bin/env bash
test () {
    echo "false"
}

if [[ "$(test)" = "true" ]]
then
    echo hello world
fi

推荐阅读