← Back to team overview

documentation-packages team mailing list archive

[Bug 1975866] [NEW] A continue or break from a subshell inside a loop no longer works.

 

Public bug reported:

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.

** Affects: ubuntu-docs (Ubuntu)
     Importance: Undecided
         Status: New

-- 
You received this bug notification because you are a member of
Documentation Packages, which is subscribed to ubuntu-docs in Ubuntu.
https://bugs.launchpad.net/bugs/1975866

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

Status in ubuntu-docs package in Ubuntu:
  New

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.

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/ubuntu-docs/+bug/1975866/+subscriptions



Follow ups