我尝试在shell脚本中使用以下语法声明一个布尔变量:

variable=$false

variable=$true

这对吗?另外,如果我想更新这个变量,我会使用相同的语法吗?最后,下面使用布尔变量作为表达式的语法正确吗?

if [ $variable ]

if [ !$variable ]

当前回答

下面是if为true的一个实现。

# Function to test if a variable is set to "true"
_if () {
    [ "${1}" == "true" ] && return 0
    [ "${1}" == "True" ] && return 0
    [ "${1}" == "Yes" ] && return 0
    return 1
}

示例1

my_boolean=true

_if ${my_boolean} && {
    echo "True Is True"
} || {
    echo "False Is False"
}

示例2

my_boolean=false
! _if ${my_boolean} && echo "Not True is True"

其他回答

我如何在shell脚本中声明和使用布尔变量?

与许多其他编程语言不同,Bash不按“类型”隔离其变量。[1]

所以答案很明确。在Bash中没有任何布尔变量。

然而:

使用declare语句,可以将赋值限制为 变量。[2]

#!/bin/bash
declare -ir BOOL=(0 1) # Remember BOOL can't be unset till this shell terminates
readonly false=${BOOL[0]}
readonly true=${BOOL[1]}

# Same as declare -ir false=0 true=1
((true)) && echo "True"
((false)) && echo "False"
((!true)) && echo "Not True"
((!false)) && echo "Not false"

declare和readonly中的r选项用于显式声明变量为只读。我希望目的很明确。

POSIX(便携式操作系统接口)

我忽略了关键的一点,那就是便携性。这就是为什么我的头文件本身有POSIX。

基本上,所有投票的答案都是正确的,除了它们太特定于bash之外。

基本上,我只想添加更多关于可移植性的信息。


像["$var" = true]中的[和]括号是不必要的,你可以省略它们,直接使用test命令: test "$var" = true && yourCodeIfTrue || yourCodeIfFalse 重要提示:我不再推荐这种方法,因为它正在慢慢被弃用,而且组合多个语句更加困难。 想象一下这些单词true和false对shell意味着什么,自己测试一下: Echo $((true)) 0 Echo $((false)) 1 但是使用引号: Echo $(("true")) Bash: "true":语法错误:操作数预期(错误令牌是"true") Sh(破折号):Sh: 1:算术表达式:期望主:""true"" 这同样适用于: Echo $(("false")) shell只能解释一个字符串。我希望你的想法是多么好的使用适当的关键字没有引号。 但在之前的回答中没有人这么说。 这是什么意思?嗯,有几件事。 你应该习惯布尔关键字实际上被视为数字,即true = 0和false = 1,记住所有非零值都被视为false。 因为它们被视为数字,你也应该这样对待它们,即如果你定义变量说: var_bool = true 回声“var_bool美元” 真正的 你可以用以下方法创建它的相反值: Var_bool =$((1 - $ Var_bool)) #与$((!美元var_bool)) 回声“var_bool美元” 1 正如您自己所看到的,shell在您第一次使用它时确实打印了true字符串,但从那以后,它都分别通过数字0表示true或1表示false来工作。


最后,你应该如何处理所有这些信息

首先,一个好习惯是赋值0而不是true;1而不是false。 第二个好习惯是测试变量是否等于0: If ["$var_bool" -eq 0];然后 yourCodeIfTrue 其他的 yourCodeIfFalse fi

我对(我自己的)愚蠢的看法:

# setting ----------------
commonMode=false
if [[ $something == 'COMMON' ]]; then
    commonMode=true
fi

# using ----------------
if $commonMode; then
    echo 'YES, Common Mode'
else
    echo 'NO, no Common Mode'
fi

$commonMode && echo 'commonMode is ON  ++++++'
$commonMode || echo 'commonMode is OFF xxxxxx'

这是一个关于在Bash中测试“布尔”值的不同方法的速度测试:

#!/bin/bash
rounds=100000

b=true # For true; b=false for false
type -a true
time for i in $(seq $rounds); do command $b; done
time for i in $(seq $rounds); do $b; done
time for i in $(seq $rounds); do [ "$b" == true ]; done
time for i in $(seq $rounds); do test "$b" == true; done
time for i in $(seq $rounds); do [[ $b == true ]]; done

b=x; # Or any non-null string for true; b='' for false
time for i in $(seq $rounds); do [ "$b" ]; done
time for i in $(seq $rounds); do [[ $b ]]; done

b=1 # Or any non-zero integer for true; b=0 for false
time for i in $(seq $rounds); do ((b)); done

它会打印出

true is a shell builtin
true is /bin/true

real    0m0,815s
user    0m0,767s
sys     0m0,029s

real    0m0,562s
user    0m0,509s
sys     0m0,022s

real    0m0,829s
user    0m0,782s
sys     0m0,008s

real    0m0,782s
user    0m0,730s
sys     0m0,015s

real    0m0,402s
user    0m0,391s
sys     0m0,006s

real    0m0,668s
user    0m0,633s
sys     0m0,008s

real    0m0,344s
user    0m0,311s
sys     0m0,016s

real    0m0,367s
user    0m0,347s
sys     0m0,017s

你可以使用shFlags。

它为您提供了定义:DEFINE_bool的选项

例子:

DEFINE_bool(big_menu, true, "Include 'advanced' options in the menu listing");

在命令行中你可以定义:

sh script.sh --bigmenu
sh script.sh --nobigmenu # False