I want to use subshells for making sure environment changes do not affect different iterations in a loop, but I'm not sure I can use loop control statements (break
, continue
) inside the subshell:
#!/bin/sh
export A=0
for i in 1 2 3; do
(
export A=$i
if [ $i -eq 2 ]; then continue ; fi
echo $i
)
done
echo $A
The value of A
outside the loop is unaffected by whatever happens inside, and that's OK. But is it allowed to use the continue
inside the subshell or should I move it outside? For the record, it works as it is written, but maybe that's an unreliable side effect.
$BASHPID
) one would expect your code to throw the usual error:continue: only meaningful in a 'for', 'while', or 'until' loop
– Archuletaexport
in the subshell is completely pointless. – Tedesco