
#!/bin/bash
VALUE=10
if [[ VALUE -eq 10 ]]
then
echo "Yes"
fi
驚いたことに「はい」が出力されます。私はそれが必要だと思いました。セクション[[ $VALUE -eq 10 ]]
を調べましたが、この動作を説明できる項目が見つかりませんでした。CONDITIONAL EXPRESSIONS
man bash
答え1
[[
bash予約語なので、などの算術バイナリ演算子を使用する代わりに、算術拡張などの特殊拡張規則が適用されます[
。-eq
したがって、シェルは整数式を探し、最初の項目でテキストを見つけたらそれを引数に拡張しようとします。算術拡張と呼ばれ、に存在しますman bash
。
RESERVED WORDS
Reserved words are words that have a special meaning to the shell.
The following words are recognized as reserved
…
[[ ]]
[[ 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.
Arithmetic Expansion
…
The evaluation is performed according to the rules listed below
under ARITHMETIC EVALUATION.
ARITHMETIC EVALUATION
…
Within an expression, shell variables may also be referenced
by name without using the parameter expansion syntax.
たとえば、
[[ hdjakshdka -eq fkshdfwuefy ]]
常に true を返します
ただし、これを行うとエラーが返されます。
$ [[ 1235hsdkjfh -eq 81749hfjsdkhf ]]
-bash: [[: 1235hsdkjfh: value too great for base (error token is "1235hsdkjfh")
再帰を使用することもできます。
$ VALUE=VALUE ; [[ VALUE -eq 12 ]]
-bash: [[: VALUE: expression recursion level exceeded (error token is "VALUE")