Bashの角かっこと二重括弧の違いは何ですか? [コピー]

Bashの角かっこと二重括弧の違いは何ですか? [コピー]

私は気づいたこの問題ある回答者は二重括弧を使用し、もう一方の回答者は大括弧を使用しました。

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

...

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

以前は二重括弧を見たことがありません。インターネット検索も役に立ちませんでした。彼らはまったく同じことを意味しますか?移植性はどのような違いをもたらしますか?他のものより1つを選ぶ理由は何ですか?

ベストアンサー1

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.

(( ))たとえば、数学的比較とビット比較を実行するだけでなく、[[ ]]より抽象的な比較(AND)を実行するためにも使用できます。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

おすすめ記事