On Fri, Jul 21, 2023 at 12:33:07AM +1000, Martin D Kealey wrote:
> C has longjmp, and other languages have exceptions. Non-local break is a
> usable "Bash shaped" analogue of those. Not perfect, sure, but close enough
> to be useful. (Non-local continue is a logical extension of that.)
> 
> Saying that this *ought* to be done using aliases is not reasonable, as it
> means forgoing the other stuff that functions can do, like taking
> parameters, declaring local variables, or returning a status.

Well in any case, the behavior you wanted is not reliable across shells,
nor even across versions of bash.


unicorn:~$ cat foo
f() { echo f; break; }

for i in 1 2 3; do
    echo "$i"
    f
done
unicorn:~$ bash-4.3 foo
1
f
unicorn:~$ bash-4.4 foo
1
f
foo: line 1: break: only meaningful in a `for', `while', or `until' loop
2
f
foo: line 1: break: only meaningful in a `for', `while', or `until' loop
3
f
foo: line 1: break: only meaningful in a `for', `while', or `until' loop
unicorn:~$ dash foo
1
f
2
f
3
f
unicorn:~$ ksh foo
1
f
2
f
3
f


Since it's not possible to do it this way portably, I suggest that the
alias alternative would be the way to go.

Reply via email to