mainThe GNU Bourne-Again SHell - Support: sr #110919, bash skips code inside nested if...


sr #110919: bash skips code inside nested if statements after "bad array subscript" error

Submitter:  None
Submitted:  Thu 10 Aug 2023 04:19:40 AM UTC
Category:  None Priority:  5 - Normal
Severity:  3 - Normal Status:  Invalid
Privacy:  Public Assigned to:  None
Originator Email:  -email is unavailable- Open/Closed:  Open
Operating System:  GNU/Linux
* Mandatory Fields

Add a New Comment Rich Markup

Thu 10 Aug 2023 02:42:16 PM UTC, comment #1: 

OK, let's unpack this. It's not a bug.

The error is one of a class called assignment errors. Assignment errors cause a non-interactive shell to abort (POSIX mode) or to return to the top level to read and execute a new command (default mode).

In this case, the executing command is the first `if' statement. It's a compound command that extends from line 5 to line 11. The assignment error causes the execution of that command to be aborted, so the next command at the top level is the `echo After outer if' at line 12.

Chet Ramey <chet>
Group administrator
Thu 10 Aug 2023 04:19:40 AM UTC, original submission:  

Trivial reproducer:
typeset -A foo=()
error () {
if true; then
  if true; then
    echo Inner if
  echo Outer if
echo After outer if

Running it

bor@bor-Latitude-E5450:~$ LC_ALL=C bash /tmp/foo
/tmp/foo: line 3: foo[""]: bad array subscript
After outer if

Moreover, when tracing with displaying line numbers, those lines disappeared

bor@bor-Latitude-E5450:~$ LC_ALL=C PS4='+$LINENO ' bash -x /tmp/foo
+1 foo=()
+1 typeset -A foo
+5 true
+6 true
+7 error
+3 foo[""]=
/tmp/foo: line 3: foo[""]: bad array subscript
+8 echo After outer if
After outer if

"echo After outer if" is line 12, not line 8.

In the real script where I noticed it this caused skipping of cleanup code inside of outer "if".

Reproduced on Ubuntu 22.04 with bash 5.1.16 and openSUSE Tumbleweed with bash 5.2.15.



(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

No files currently attached


Depends on the following items: None found

Items that depend on this one: None found


Carbon-Copy List
  • -email is unavailable- added by chet (Posted a comment)
  • -email is unavailable- added by None (Submitted the item)

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.


    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-08-10 chet StatusNone Invalid

    Back to the top

    Powered by Savane 3.13-f762.
    Corresponding source code