I have a bash script env_setter.sh. It sets an environment variable. It contains:
#!/bin/bash echo "inside env_setter script....." export NAME="jake" I have another script tester.sh. It tries to print the value of the environment variable set by env_setter.sh. It contains:
#!/bin/bash echo "Inside tester script..." echo "$NAME" Then I have a script runner.sh that executes both of the above scripts as follow:
#!/bin/bash echo "Inside runner script..." . ./env_setter.sh echo "$NAME" sudo nohup ./tester.sh > demo.log 2>&1 & echo $! > save_pid.pid Now when I run runner.sh, I get following output in demo.log:
inside env_setter script..... jake Inside tester script... As we can see, the last echo command in tester.sh doesn't print anything. This is because the environment variable that we set with env_setter.sh is not being exported inside the context of tester.sh.
So, how do I export the environment variable to the background process in such cases?
sudo -Eto preserve the calling environment. Or you can pass variables on the command line,sudo NAME=jake.sudo -Eworked for me. Thanks !