Was ist der Unterschied zwischen eckigen Klammern und doppelten Klammern in Bash?

Was ist der Unterschied zwischen eckigen Klammern und doppelten Klammern in Bash?

Ich merke indiese Fragedass ein Antwortender doppelte Klammern verwendet hat, während der andere eckige Klammern verwendet hat:

if (( $(fileSize FILE1.txt) != $(fileSize FILE2.txt) )); then

...

if [ $(fileSize FILE1.txt) != $(fileSize FILE2.txt) ]; then

Ich habe noch nie doppelte Klammern gesehen – und googeln hilft nicht weiter. Haben sie genau dieselbe Bedeutung? Gibt es einen Unterschied in der Portabilität? Gründe, das eine dem anderen vorzuziehen?

Antwort1

Aus der bashManpage:

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

Sie verwenden (( ))für mathematische und bitweise Vergleiche sowie [[ ]]für abstraktere Vergleiche (mit test file attributes and perform string and arithmetic comparisons), zum Beispiel

touch test;
if [ -e test ]; then
    echo test exists
else
    echo test does not exist
fi

verwandte Informationen