I have the following pattern in a bash script:
#!/bin/bash
a="Default"
the_function() {
echo "Something"
a="Set By Function"
}
Just a function that alters the value of a global variable.
If I call it, it works as intented, the following call (also in the same script):
echo $a
the_function
echo $a
Prints the expected:
Default
Something
Set By Function
But if I call the function and pipe its output, the variable is not altered (outside of the function):
This call:
echo $a
the_function | tee my-log-file.log
echo $a
The output:
Default
Something
Default
What is the difference in environment between a function call and a function call that pipes the output?
And how can I pipe the output and still have the variable altered?
Try
the_function >( tee my-log-file.log )
to avoid the pipe.