A continue or break from a subshell inside a loop no longer works.

Bug #1975866 reported by Johnny Klonaris
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
ubuntu-docs (Ubuntu)
Invalid
Undecided
Unassigned

Bug Description

The following code works in bash on any number of Linux distros, including
CentOS 6.10 - 7.8 and Ubuntu 14.04 - 18.04.

#!/bin/bash
#
# Test for continue from subshell issue...
#
echo SHELL is $SHELL
for i in $(seq 5)
do
    (
    echo loop $i
    if [[ $i == 2 ]] ; then
        echo i = $i - continue
        continue
    fi

    if [[ $i == 4 ]] ; then
        echo i = $i - exit
        exit 1
    fi
    echo Loop $i subshell end
    )
    rc=$?
    echo Loop $i rc $rc
done

--
The output on all of those machines is:

SHELL is /bin/bash
loop 1
Loop 1 subshell end
Loop 1 rc 0
loop 2
i = 2 - continue
Loop 2 rc 0
loop 3
Loop 3 subshell end
Loop 3 rc 0
loop 4
i = 4 - exit
Loop 4 rc 1
loop 5
Loop 5 subshell end
Loop 5 rc 0

---
On a Ubuntu 20.04 machine:

SHELL is /bin/bash
loop 1
Loop 1 subshell end
Loop 1 rc 0
loop 2
i = 2 - continue
packages/tmp/looptest: line 14: continue: only meaningful in a `for', `while',
or `until' loop
Loop 2 subshell end
Loop 2 rc 0
loop 3
Loop 3 subshell end
Loop 3 rc 0
loop 4
i = 4 - exit
Loop 4 rc 1
loop 5
Loop 5 subshell end
Loop 5 rc 0

--
If I change the shebang to: #!/bin/sh
the behavior is the same as the first example - except - on our machines, we
specifically link /bin/sh to bash:

$ ls -l /bin/sh
lrwxrwxrwx 1 root root 9 May 12 22:04 /bin/sh -> /bin/bash

Which seems just... weird.

This behavior seems to have started after a recent update to Ubuntu 20.04.

I realize that the argument can be made the new behavior is more technically
correct, but using a subshell to isolate vars between loops can be very helpful
and the fact that this has worked until now is concerning.

Revision history for this message
Gunnar Hjalmarsson (gunnarhj) wrote :

This is not a bug about the Ubuntu Desktop Guide, so closing.

I would recommend you to show your observation at Ask Ubuntu. If you then conclude that it's a bug, please report it against the bash package.

Changed in ubuntu-docs (Ubuntu):
status: New → Invalid
Revision history for this message
Johnny Klonaris (jklonaris) wrote :

Gunnar - thanks. I realized that I might have filed it in the wrong place - my apologies.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.