Continuing Semicolon in conditional structures (which handles single brackets), what's the point of having a semicolon after the closing DOUBLE bracket ]]?
In my tests, running
#!/bin/zsh -- if [[ "a" == "a" ]] then echo "true" else echo "false" fi if [[ "a" == "a" ]]; then echo "true" else echo "false" fi if [[ "a" == "a" ]] then echo "true" else echo "false" fi if [[ "a" == "a" ]]; then echo "true" else echo "false" fi yields
true true true true , and running
#!/bin/zsh -- if [[ "a" == "b" ]] then echo "true" else echo "false" fi if [[ "a" == "b" ]]; then echo "true" else echo "false" fi if [[ "a" == "b" ]] then echo "true" else echo "false" fi if [[ "a" == "b" ]]; then echo "true" else echo "false" fi yields
false false false false No error is reported. In zsh, what's the difference between the conditionals with a semicolon ; after the closing double bracket and the conditionals without a semicolon after the closing double bracket?
The same question goes for bash.
thenmust be at the beginning of a line or otherwise might be interpreted as a literal string ... Compare for example:if true then echo "true"; else echo "false"; fitoif true; then echo "true"; else echo "false"; fi[[is a keyword (trytype [[) while[is an alias for the commandtestso considered a command much liketruein my example above.if true then echo "true"; else echo "false"; fiyields “zsh: parse error near `else'”. In bash, we get “bash: syntax error near unexpected token `else'”. That's confusing. What's the reason for the necessity of the semicolon here aftertruebut not in the examples in the OP?true then echo "true"is as valid a regular command asecho if you do this, then that happens. Shell keywords are only recognized as such at the start of a command, so that's similar toif echo foo; else echo bar; fi. It's missing the mandatorythen ...part, so a syntax error. But[[ .. ]]and(( .. ))andif ... fiand such are compound constructs where end is marked by the closing token. For regular (simple) commands, the semicolon (or newline) is needed to mark the end.