Re: Missing semicolon tolerated after double-bracket [[ ]] expression
From:
Koichi Murase
Subject:
Re: Missing semicolon tolerated after double-bracket [[ ]] expression
Date:
Thu, 11 Sep 2025 07:42:53 +0900
2025年9月11日(木) 7:02 Martin Jambon <mjambon@gmail.com>:
> Description:
> Expectation: I expect a syntax error in the absence of a semicolon
> or a newline between the closing brackets and the then.
>
> Behavior with Bash 5.2.21: no error, the program behaves as if a
> semicolon was present.
This is required by the POSIX standard [1-4]. Although [[ ... ]] is
not technically a part of POSIX, it is a compound at the same level as
(...), and (...) without a semicolon is allowed here.
[1] https://lists.gnu.org/archive/html/bug-bash/2021-02/threads.html#00135
[2]
https://pubs.opengroup.org/onlinepubs/9799919799/xrat/V4_xcu_chap01.html#tag_23_02_10
[3]
https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_10_02
[4]
https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_04
The behavior of Bash < 5.2 was simply inconsistent.
$ zsh -c 'if [[ -n a ]] then echo hello; fi'
hello
$ ksh -c 'if [[ -n a ]] then echo hello; fi'
hello
$ mksh -c 'if [[ -n a ]] then echo hello; fi'
hello
$ yash -c 'if [[ -n a ]] then echo hello; fi'
hello
$ bash-5.1 -c 'if [[ -n a ]] then echo hello; fi'
bash-5.1: -c: line 1: syntax error near unexpected token `then'
bash-5.1: -c: line 1: `if [[ -n a ]] then echo hello; fi'
$ bash-5.2 -c 'if [[ -n a ]] then echo hello; fi'
hello
--
Koichi