This is a little thing that tripped me up, and I'd like to write this blog post so that I don't forget it!
One Use Case- Setting Environment Variables From A Bash Script
In a unix-based shell I like to set environment variables with the "export" command. I was working on a project where I had a bunch of environment variables that needed to be exported, and I wanted to put them all in a single bash file.
So, I created a file called "set-env.sh" that looked something like this:
Now I could just run one command and my environment variables would be all set. Yay!
I went and happily ran my bash script like this:
But to my horror when I then went to check if the variables had the proper values (by running, for example, echo $bob) I found that they were undefined!
Dot-Space To The Rescue!
Thanks to mbaird and his stack overflow answer here. He noted that you need to run the script, "as source" which can be done by prefixing your invocation of the file by either the word "source" or a dot. So, moral of the story is if you do it like this then it should work!
The posts on this site are written and maintained by Jim Lynch. About Jim...