If you want to set an environment variable in your Mac OS system then for this you have to use the export variable_name=value command. And if the value contains spaces then the value must be enclosed in double quotes; In this tutorial, we will show you how to set environment variables in Mac OS using the zsh variable_name command on the terminal.
How to Set Environment Variables in Mac Os using ZSH Terminal
Here are some steps to set environment variables in Mac using zsh command on terminal:
- Step 1: Start Terminal
- Step 2: Specify the Shell
- Step 3: Edit Shell Profile File
- Step 4: Set/Add Environment Variables Mac
- Step 5: Reload Profile
- Step 6: Verify the Environment Variable
Step 1: Start Terminal
Firstly, Press Cmd + Space
to open Spotlight Search and Type “Terminal” or “Terminal.app” into the search bar.
Step 2: Specify the Shell
Now, you need to run the following command on terminal to can check your current shell:
echo $SHELL
Step 3: Edit Shell Profile File
Two common shell profile files are .bash_profile (for Bash) and .zshrc (for Zsh). you can use nano or vi text editor to edit the file. For example, if you are using Bash, you can run:
nano ~/.bash_profile
For Zsh shell:
nano ~/.zshrc
Step 4: Set/Add Environment Variables Mac
Add your environment variables in the format export VARIABLE_NAME="variable_value"
. For example, to set a database connection variable:
export YOUR_VARIABLE_NAME="Your Variable Value"
In Nano, you can save your changes by pressing Ctrl + O, then press Enter. To exit, press Ctrl + X.
Step 5: Reload Profile
For the changes to take effect, you can either restart your terminal and execute the following command:
source ~/.bash_profile
Step 6: Verify the Environment Variable
Now, you need to run the following commands on the terminal to verify that the environment variable is set:
echo $MY_VARIABLE
Conclusion
That’s it! You’ve successfully set and accessed environment variables in mac os using terminal zsh.