bash – Why do I need a closing square bracket in if-blocks?

Question:

If the opening parenthesis is synonymous with the test command, then my question arises – why do we need a closing parenthesis?

if [ $a -eq $b ]

Later versions of bash support the [[…]] (reserved word) construct. What are the differences?

Why does an error occur when using […] and several conditions (&&, ||), but when using the construction [[…]], an error does not occur?

if [ $a -eq $b && $c -eq $d ] - ошибка
if [[ $a -eq $b && $c -eq $d ]] - не ошибка

UPD: interpreter #! / Bin / bash is used.
* .Sh format scripts

Answer:

info taken from man bash .

  1. let's start with the terminology.
    1. [[ Is a compound command )
    2. [ and test are just builtin commands
  2. a compound command (their list is far from being limited to the [[ ) command is characterized by the fact that its argument can be a compound expression that combines expressions using the following operators (listed in order of priority of calculation):
    1. ( выражение ) – grouping expressions to change the order of evaluation / execution
    2. ! выражение – "not"
    3. выражение1 && выражение2 – "and"
    4. выражение1 || выражение2 – "or"
  3. the command [ not exactly a synonym for the test command. the syntax for calling them is slightly different, although they are functionally identical:

     test выражение [ выражение ]
  4. the built-in commands [ and test have similar, but slightly different syntax, operators for grouping, negation, and logical and / or operations:
    1. \( выражение \) – grouping
    2. ! выражение – "not" (here the syntax is identical)
    3. выражение1 -a выражение2 – "and"
    4. выражение1 -o выражение2 – "or"
Scroll to Top