bash"if [false];";返回true而不是false-为什么呢? [英] bash "if [ false ];" returns true instead of false -- why?
本文介绍了bash"if [false];";返回true而不是false-为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么以下输出为 True
?
#!/bin/sh
if [ false ]; then
echo "True"
else
echo "False"
fi
这将始终输出 True
,即使条件似乎表明并非如此.如果我删除括号 []
,则可以使用,但是我不明白为什么.
This will always output True
even though the condition would seem to indicate otherwise. If I remove the brackets []
then it works, but I do not understand why.
推荐答案
您正在运行带有参数"false"的 [
(aka test
)命令,但未运行命令 false
.由于"false"是非空字符串,因此 test
命令始终成功.要实际运行该命令,请放下 [
命令.
You are running the [
(aka test
) command with the argument "false", not running the command false
. Since "false" is a non-empty string, the test
command always succeeds. To actually run the command, drop the [
command.
if false; then
echo "True"
else
echo "False"
fi
这篇关于bash"if [false];";返回true而不是false-为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文