When setting up a development environment, it's necessary to set external variables for various purposes. If you need to set environment variables when setting up a local development environment on
Mac or Linux distributions? In this case, I want to briefly understand the differences and methods from other environments.
! Purpose of Setting Environment Variables
First, environment variables in a development environment are used for various purposes. Especially in a development environment where version control like Git is used, values such as tokens required for security are mostly stored in external environment variable settings and are not included in version control. Alternatively, you can use libraries like
dotenv which are only usable locally by using an env file.
! how to set environment variables on a Mac
Below, we'll look at how to set environment variables on a Mac. For reference, Windows provides the most convenient GUI for setting environment variables, making it the easiest and simplest. And for
WSL 2 or Linux environments, you can declare variables in the same way as on Mac using methods like the
.bashrc file.
# Setting Environment Variables in Mac terminal or Linux
In the Mac terminal or Linux distribution environment, there are a few methods including .bashrc.
.bashrc
.bash_profile
.zshrcAll these files are located in the user's home directory. You can move using
cd ~ and then declare and use variables in the desired file. If any of these files don't exist, you should create them using commands like touch.
[ Tip ]The .bash_history file also exists in the home directory, and if you check its contents, you can see the history of the commands you've entered.What is the difference between .bashrc and .bash_profile? .bash_profile is a
script that runs when logged in, and in contrast,
.bashrc runs every time a new terminal is opened. You can use each depending on the situation and purpose.
! When to Use .zshrc
If you open a terminal and see
- zsh indicated in the terminal window, it means the default shell is zsh. In this case, instead of .bash_profile, use .zshrc to declare variables. If the file doesn't exist, you should create the
.zshrc file in the same way.
$ touch .zshrc
$ vim .zshrc
After creating the file, you need to declare the necessary variables. For example, you can create variables with specific names like:
MY_NAME="WEBISFREE.com"
MY_CUSTOM_PATH="/path/to/my/site"
Here, we've created two variables, MY_NAME and MY_CUSTOM_PATH.
Restart After Saving .zshrc File
After creating and saving the file, you need to check if it's applied correctly. Before checking, you may need to restart, or you can reload the file as follows:
$ source ~/.zshrc
Once restarted, you'll need to check if the declared variables are being used correctly. When reading and using the variables again, you can declare and use them as follows. Here's how to print the MY_NAME variable and see the result:
echo ${MY_NAME}
or
echo $MY_NAME
Both of the above methods are possible. When executed, the following was displayed:
// Result
WEBISFREE.com
As expected, we could confirm that it works correctly.
Up to this point, we've briefly looked at how to declare and use environment variables in the Mac terminal and Linux.