help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] break and continue inside subshells


From: DJ Mills
Subject: Re: [Help-bash] break and continue inside subshells
Date: Fri, 19 Oct 2012 12:15:14 -0400

On Thu, Oct 18, 2012 at 7:12 PM, Glenn Morris <address@hidden> wrote:
>
> Hi,
>
> I found this behaviour a little odd (bash 4.2.37 on RHEL6):
>
> ## This is fine:
> ( echo 1; break; echo 2 )
>
>   -> Prints "1", bash: break: only meaningful in a `for', `while', or
>   `until' loop, "2"
>
> So a "break" inside a subshell has no effect (as expected).
>
>
> ## This is suprising:
> for f in 1 2; do
>   (
>     echo $f
>     break
>     echo X
>   )
>   echo Y
> done
>
> It prints "1", "Y", "2", "Y".
> So now the break does have an effect, but not the expected one. It just
> jumps to the end of the subshell, not the next loop iteration. ksh and
> zsh seem to behave in the same way, but it seems odd to me...
>
>

Personally, I'd just exit the subshell > 0 and check its status.

for f in 1 2; do
  (
    echo "$f"
    exit 1
    echo X
  ) || break
  echo Y
done



reply via email to

[Prev in Thread] Current Thread [Next in Thread]