Variables are not saved when using a pipe because both sides are in a
subshell environment.

A common place where a user may encounter this is when dealing with
multiple items:

bash bug??
>
> Does not change the value of $new:
>
> echo hi| for i in 3; do new=3; done
>
>
> reason for piping:
> echo wat | for i in $(< /dev/stdin); do declare -g new="$i"; done
>
> (using find instead of echo, not sure if better way to loop)
>

Although a user may not expect variable assignments in a subshell to
persist in the global environment, new users don't expect pipe to create
subshells so these subshells should assign to global variables. It's easy
to explicitly create subshells so this increases options without removing
any.

The way to assign variables that persist in this case is supposed to be to
connect the commands in some other way but many new users learn of the pipe
operator and try to use it.

Reply via email to