franki3xe franki3xe - 2 months ago 10
Bash Question

How to source file from bash script

I'm trying to source a file with an environment variable from my bash script, but it doesn't work.

This is the content of my script (
), which is located in

touch $FILE_NAME
echo "export TEST=\"test\"" > $FILE_NAME
source $FILE_NAME

Then I use
my ~/.bashrc

alias testScript=~/scripts/

But when I use my script
, it didn't set the environment variable.


Environment variables only flow downstream in the process tree.

When you type testScript to a bash process, it creates a child process and execs /bin/bash or whatever is set by #!

Any environment variables set there remain only with the child process. Export causes the variables to be copied to additional grandchildren (children of that child) that might be spawned from that child.

Nothing can copy back to a parent. You need to use source instead of running the file. See Jonathan's answer.

You could try editing the files ~/.bashrc or ~/.login to set enviornment variables you need frequently.

See also and for more explanation of export in bash.