bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Pipe using subshells, variables not saved


From: Misaki
Subject: Pipe using subshells, variables not saved
Date: Fri, 3 Mar 2017 22:04:23 -0800

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

[Prev in Thread] Current Thread [Next in Thread]