BASH: Global variables aren't updateable in a function only when that function is piped (simple example)

David Parks picture David Parks · Jul 12, 2011 · Viewed 12.7k times · Source

This smells buggy, but probably, someone can explain it:

The following script doesn't work, the output is below:

#!/bin/bash
GLOBAL_VAR="OLD"
myfunc() {
        echo "func before set> $GLOBAL_VAR"
        GLOBAL_VAR="NEW"
        echo "func after set> $GLOBAL_VAR"
}
myfunc | cat
echo "final value> $GLOBAL_VAR"

Output:

func before set> OLD
func after set> NEW
final value> OLD

Now, just take off the | cat and it works!

#!/bin/bash
GLOBAL_VAR="OLD"
myfunc() {
        echo "func before set> $GLOBAL_VAR"
        GLOBAL_VAR="NEW"
        echo "func after set> $GLOBAL_VAR"
}
myfunc
echo "final value> $GLOBAL_VAR"

Output:

func before set> OLD
func after set> NEW
final value> NEW

Answer

Rajish picture Rajish · Jul 12, 2011

A pipe creates a subshell. It's said in the bash manual that subshells cannot modify the environment of their parents. See these links:

http://www.gnu.org/software/bash/manual/bashref.html#Pipelines

http://wiki.bash-hackers.org/scripting/processtree#actions_that_create_a_subshell