One Use Case- Setting Environment Variables From A Bash Script
So, I created a file called "set-env.sh" that looked something like this:
export bob=hello export frank=tony
The Horrors!
./set-env.sh
Dot-Space To The Rescue!
. ./set-env.sh
Writings about one coder's stories & experiences.
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: export bob=hello export frank=tony
Now I could just run one command and my environment variables would be all set. Yay!
The Horrors!
I went and happily ran my bash script like this:
./set-env.sh
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!
. ./set-env.sh
0 Comments
Your comment will be posted after it is approved.
Leave a Reply. |
AuthorThe posts on this site are written and maintained by Jim Lynch. About Jim...
Categories
All
Archives
March 2023
|