Here’s a gotcha that caught me out this week.
I had code like this, used to source settings from scripts stored in another directory:
find /etc/application -name "*.sh" -type f | while read FILE; do
source $FILE
done
exec /path/to/application/run.shInside /etc/application/set_name.sh I’d have something like:
export SOME_VARIABLE="some value"But when the application ran, it never saw the value of SOME_VARIABLE. Puzzling.
The reason: bash pipes run in subshells. The while read loop on the right side of the pipe runs in a subshell, so that’s where source executes. And subshells can’t modify the environment of their parent process. The exported variables vanish the moment the subshell exits.
The fix is to make sure source runs in the main process. You can do this with process substitution and input redirection instead of a pipe:
while read FILE; do
source $FILE
done < <(find /etc/application -name "*.sh" -type f)
exec /path/to/application/run.shNow the while read loop runs in the main shell, source sets the variables in the right place, and the application sees everything it expects.