Why is no semicolon required in these cases?

The answer to this question puzzled me. "They are all wrong!" was my thought, since I had previously omitted the semicolon in the same or similar cases as the OP, and everything worked fine.

Compound command documentation has shown that these answers are correct:

{ list; }

A list with a semicolon (or new line) is required.

... he talks about grouping commands and a similar thing for loops:

Note that wherever a; appears in the description of the command syntax, it can be replaced by one or more newline characters.

... and all examples use semicolons.

But , in practice, you can freely omit the semicolon when nesting loops, if curly braces, etc., in any order. For instance:

{ if true; then echo; fi }
# no ";" here           ^
for i in {1..3}; do if true; then echo "$i"; fi done
# no ";" here                                  ^
{ while true; do if true; then for i in {1..3}; do { echo "$i"; } done fi done }
# no ";" in these 4 places                                       ^    ^  ^    ^

I tested it on bash 4.1, 4.2, 4.4 and dash 0.5.7, they all work sequentially.

My question is : is this just a feature of the implementation, and I should not rely on it, or a standardized behavior that you can rely on? (Bash docs don't say anything about this case).

+4
source share
1 answer

While writing this question, I found information that seems to answer it.

" " ( ";" ), , , , - POSIX.

:

!     {     }      case
do    done  elif   else
esac  fi    for    if
in    then  until  while

, , :

  • ...
  • , , case, for in
  • ...

, , } done fi done }, POSIX, .

+6

Source: https://habr.com/ru/post/1693071/


All Articles