Reputation: 4327
x=0; { x=1; echo $x; } | cat; echo $x
prints
1
0
while I expected
1
1.
Why? At last, curly brackets create no subshell.
I tested with bash and busybox sh (ash).
Upvotes: 0
Views: 74
Reputation: 246744
In bash both sides of a pipeline are run in subshells (https://www.gnu.org/software/bash/manual/bashref.html#Pipelines) unless you shopt -s lastpipe; set +m
, where the last command in a pipe is executed in the current shell
$ sum=0; seq 10 | while read n; do ((sum+=n)); done; echo $sum
0
$ shopt -s lastpipe
$ sum=0; seq 10 | while read n; do ((sum+=n)); done; echo $sum
0
$ set +m
$ sum=0; seq 10 | while read n; do ((sum+=n)); done; echo $sum
55
In your example, the first command in the pipeline will always be run in a subshell.
I can't speak for ash
Upvotes: 2