ablog

不器用で落着きのない技術者のメモ

bash の if 文の「 [ ] 」と「 [ [ ] ] 」の違い

  • 「[ 」は外部コマンド。/usr/bin 以下にある実行可能ファイル。「 [ 」がコマンド本体で「 ] 」はパラメータ。 「 [ 」の直後にスペースがないとエラーになるのはそのため。
$ which [
/usr/bin/[
$ ls -l /usr/bin/[
-rwxr-xr-x 1 root root 31048 Sep  4 07:09 /usr/bin/[
$ file /usr/bin/[
/usr/bin/[: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped
  • 「[ [」はbash の組み込みコマンド
$ which [[
/usr/bin/which: no [[ in (/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/neo/bin)
$ man 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  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 substitution, and  quote  removal  are  performed.
              Conditional operators such as -f must be unquoted to be recognized as primaries.

              When the == and != operators are used, the string to the right of the operator is considered a pattern and matched according to the rules described
              below under Pattern Matching.  If the shell option nocasematch is enabled, the match is performed without regard to the case of alphabetic  charac-
              ters.  The return value is 0 if the string matches (==) or does not match (!=) the pattern, and 1 otherwise.  Any part of the pattern may be quoted
              to force it to be matched as a string.

              An additional binary operator, =~, is available, with the same precedence as == and !=.  When it is used, the string to the right of  the  operator
              is  considered  an  extended regular expression and matched accordingly (as in regex(3)).  The return value is 0 if the string matches the pattern,
              and 1 otherwise.  If the regular expression is syntactically incorrect, the conditional expression’s return  value  is  2.   If  the  shell  option
              nocasematch is enabled, the match is performed without regard to the case of alphabetic characters.  Substrings matched by parenthesized subexpres-
              sions within the regular expression are saved in the array variable BASH_REMATCH.  The element of BASH_REMATCH with index 0 is the portion  of  the
              string  matching  the  entire regular expression.  The element of BASH_REMATCH with index n is the portion of the string matching the nth parenthe-
              sized subexpression.

              Expressions may be combined using the following operators, listed in decreasing order of precedence:

              ( expression )
                     Returns the value of expression.  This may be used to override the normal precedence of operators.
              ! expression
                     True if expression is false.
              expression1 && expression2
                     True if both expression1 and expression2 are true.
              expression1 || expression2
                     True if either expression1 or expression2 is true.

              The && and || operators do not evaluate expression2 if the value of expression1 is sufficient to determine the return value of  the  entire  condi-
              tional expression.

なーるほど。