空白扩展测试`[[]]`不起作用

Blank extended test `[[ ]]` doesn't work

我可以在 test 和算术扩展 ((...)) 中使用空白表示 NULL,例如

$ test ; echo $?
1
$ (( )); echo $?
1

但是使用扩展测试命令,我得到一个错误,

$ [[ ]]; echo $?
bash: syntax error near `;'

好的,我可以使用 ''"" 来修复这个错误,但我的问题是

  1. 有什么方法可以使用空白测试[[ ]]而不出错吗?
  2. 如果 1. 的答案是 "no",那么我想知道 [[ ]] 的行为与 [ ](( )) 不同的原因?

关于你的第一个问题,为什么只有 [[ ]] 在我阅读 BASH 的手册页时不起作用,我看到以下内容:

[[…]] [[ expression ]] Return a status of 0 or 1 depending on the evaluation of the conditional expression expression. Expressions are composed of the primaries described below in Bash Conditional Expressions. Word splitting and filename expansion are not performed on the words between the [[ and ]]; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are performed. Conditional operators such as ‘-f’ must be unquoted to be recognized as primaries.

这意味着它不应该是空的。

对于你的第二个问题,[[[ 之间的区别是 [[[ 的增强,这里非常有用所以 link 我发现你可以也通过它。

What's the difference between [ and [[ in Bash?

编辑: 为了检查最后一个命令的状态,我创建了一个简单的检查,您也可以将其作为开始:

if [[ $? -eq 0 ]]
then
  echo "Last command ran successfully."
else
  echo "Last command status is NOT 0 so could be it did not run properly."
fi

EDIT2: 根据 OP 在评论中的问题,也为 (( 添加示例。 ((.

的使用示例
cat script.ksh
echo "Please enter a value:"
read value
if ! ((value % 4)); then
    echo "$value is fully divided from 4."
fi

(( 来自 man BASH:

((expression)) The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION. If the value of the expression is non- zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to let "expression".