Bash中的方括号和双括号有什么区别?

Ste*_*ett 2 bash test

我注意到在这个问题中,一个回答者使用了双括号,而另一个回答者使用了方括号:

if (( $(fileSize FILE1.txt) != $(fileSize FILE2.txt) )); then
Run Code Online (Sandbox Code Playgroud)

...

if [ $(fileSize FILE1.txt) != $(fileSize FILE2.txt) ]; then
Run Code Online (Sandbox Code Playgroud)

我以前从未见过双括号 - 谷歌搜索也无济于事。它们的含义完全相同吗?便携性有区别吗?偏爱其中一个的原因?

小智 6

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".

   [[ 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  under  CONDITIONAL  EXPRESSIONS.  Word splitting and pathname expansion are not performed on the words
          between the [[ and ]]; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process  sub?
          stitution, and quote removal are performed.  Conditional operators such as -f must be unquoted to be recognized as primaries.
Run Code Online (Sandbox Code Playgroud)

您可以使用(( ))进行数学和位比较,并[[ ]]进行更抽象的比较(至test file attributes and perform string and arithmetic comparisons),例如

touch test;
if [ -e test ]; then
    echo test exists
else
    echo test does not exist
fi
Run Code Online (Sandbox Code Playgroud)