A command inside a pipeline (that is, a series of commands separated by |
) is always executed in a subshell, which means that each command has its own variable environment. The same is true of the commands inside the compound command (…)
, but not the compound command {…}
, which can normally be used for grouping without creating a subshell.
In bash
or zsh
, you can solve this problem using process substitution instead of a pipeline. For example:
andy="10"
for ((i=0 ; i <= 100 ; i+=50)); do
andy="20"
echo $i
sleep 1
done > >(whiptail --gauge "Please wait" 6 50 0)
echo "My val $andy
>(whiptail ...)
will cause a subshell to be created to execute whiptail
; the entire expression will be substituted by the name of this subshell's standard input (in linux, it will be something like /dev/fd/63
, but it could be a FIFO on other OSs). > >(...)
causes standard output to be redirected to the subshell's standard input; the first >
is just a normal stdout redirect.