Mac OS Recipe: How to Set Up Environment Variables
Most of the time I use Mac for my programming stuff but sometime I use Windows too. In my experience, setting up environment variables and path is lot more straight-forward and simpler on Windows than in Unix-based operating systems like Mac.
I wanted to create a quick recipe for folks who use Mac and want to learn how to work with environment variables.
There are number of ways you add or edit environment variables on Mac and thats probably why it can be confusing for some. We are not going to make it complicated. We would keep it simple. We would just work with the .bash_profile file to create some new environment variables.
- Open Terminal
- Firstly, you need to execute the following command to create the .bash_file if you don’t have it:
If the file already exist, nothing will happen otherwise it will be created.
- Now, lets open the file in the text editor so that we can add some environment variables:
- Let’s create an environment variable called AUTHOR_NAME and give it some value:
- Save the file.
- Back in the Terminal, execute the following command to let the Terminal know that things have changed in the ~/bash_profile file:
Note: With this command, you don’t have to close the Terminal window and open it again in order to read the newly added or updated environment variables.
- Now, you can easily read the newly added variable by executing the command
echo $AUTHOR_NAME. The character $ in front of the variable tells the echo command that you mean AUTHOR_NAME environment variable and not AUTHOR_NAME as text.
- You can list all your environment variables by executing the env command which should include the variables that you have defined in the .bash_profile file.
If you want to delete the environment variable, you just open the .bash_profile file again and simply delete the line you added.