为什么以下输出为True?

#!/bin/sh

if [ false ]; then
    echo "True"
else
    echo "False"
fi

这将始终输出True,即使条件似乎另有指示。如果我去掉括号[],它就可以工作了,但我不明白为什么。


当前回答

正如@tripleee所指出的,这充其量是切线。

不过,如果你来到这里寻找类似的东西(就像我一样),这是我的解决方案

必须处理用户可访问的配置文件,我使用这个函数:

function isTrue() {
    if [[ "${@^^}" =~ ^(TRUE|OUI|Y|O$|ON$|[1-9]) ]]; then return 0;fi
    return 1
    }

Wich可以这样用

if isTrue "$whatever"; then..

您可以更改regexp中的“true列表”,本例中的“true列表”与法语兼容,并将诸如“yes, yes, on,1, Oui,y,true”之类的字符串视为“true”。

注意,'^^'不区分大小写

其他回答

我发现我可以做一些基本的逻辑运行如下:

A=true
B=true
if ($A && $B); then
    C=true
else
    C=false
fi
echo $C

Bash的快速布尔入门

if语句接受命令作为参数(如&&,||等)。该命令的整数结果码被解释为布尔值(0/null=true, 1/else=false)。

test语句以操作符和操作数作为参数,并以与if相同的格式返回结果代码。测试语句的别名是[,它经常与if一起使用,以执行更复杂的比较。

true和false语句不执行任何操作,并返回结果代码(分别为0和1)。所以它们可以在Bash中用作布尔字面值。但是如果你把语句放在一个它们被解释为字符串的地方,你就会遇到问题。在你的情况下:

if [ foo ]; then ... # "if the string 'foo' is non-empty, return true"
if foo; then ...     # "if the command foo succeeds, return true"

So:

if [ true  ] ; then echo "This text will always appear." ; fi;
if [ false ] ; then echo "This text will always appear." ; fi;
if true      ; then echo "This text will always appear." ; fi;
if false     ; then echo "This text will never appear."  ; fi;

这类似于echo '$foo' vs. echo "$foo"。

当使用test语句时,结果取决于所使用的操作符。

if [ "$foo" = "$bar" ]   # true if the string values of $foo and $bar are equal
if [ "$foo" -eq "$bar" ] # true if the integer values of $foo and $bar are equal
if [ -f "$foo" ]         # true if $foo is a file that exists (by path)
if [ "$foo" ]            # true if $foo evaluates to a non-empty string
if foo                   # true if foo, as a command/subroutine,
                         # evaluates to true/success (returns 0 or null)

简而言之,如果你只是想测试一些pass/fail(又名“true”/“false”)的东西,然后向你的if或&&等语句传递一个命令,不带括号。对于复杂的比较,使用括号和适当的操作符。

是的,我知道在Bash中没有原生布尔类型这样的东西,如果和[和true在技术上是“命令”而不是“语句”;这只是一个非常基本的功能解释。

您正在运行带有参数“false”的[(又名test)命令,而不是运行命令false。由于"false"是非空字符串,test命令总是成功。要实际运行该命令,请删除[命令。

if false; then
   echo "True"
else
   echo "False"
fi

使用true/false删除一些括号混乱…

#! /bin/bash    
#  true_or_false.bash

[ "$(basename $0)" == "bash" ] && sourced=true || sourced=false

$sourced && echo "SOURCED"
$sourced || echo "CALLED"

# Just an alternate way:
! $sourced  &&  echo "CALLED " ||  echo "SOURCED"

$sourced && return || exit

正如@tripleee所指出的,这充其量是切线。

不过,如果你来到这里寻找类似的东西(就像我一样),这是我的解决方案

必须处理用户可访问的配置文件,我使用这个函数:

function isTrue() {
    if [[ "${@^^}" =~ ^(TRUE|OUI|Y|O$|ON$|[1-9]) ]]; then return 0;fi
    return 1
    }

Wich可以这样用

if isTrue "$whatever"; then..

您可以更改regexp中的“true列表”,本例中的“true列表”与法语兼容,并将诸如“yes, yes, on,1, Oui,y,true”之类的字符串视为“true”。

注意,'^^'不区分大小写