How to Set Environment Variables in Bash and Zsh
Setting environment variables in both Bash and Zsh is a straightforward process. However, there is a slight difference when it comes to persisting them. In Bash, you can use the .bashrc
file, while in Zsh, you can use the .zshrc
file.
To set environment variables in the shell, follow these steps:
Open your terminal and type the following command:
1
$ export VARIABLE=something
This command sets the value of the variable “VARIABLE” to “something”.
To ensure that the variable was set correctly, you can check its value by typing:
1
$ echo $VARIABLE
This command will display the value of the “VARIABLE” variable.
If you make changes to a dot file (e.g.,
.bashrc
or.zshrc
), you need to apply the changes to the current shell session using thesource
command. For example:1
$ source .bashrc
This command reloads the “.bashrc” file and applies the changes without the need to restart the shell.
The steps outlined above are applicable for both Bash and Zsh.
For Fish shell users, the process is slightly different. To set environment variables in Fish, you need to prepend the env
command. For example:
1 | env API_KEY=123123 node app.js |
This command sets the “API_KEY” environment variable to “123123” and runs the “app.js” script using Node.js.
By following these steps, you can easily set environment variables in Bash, Zsh, and Fish shells.
Tags: environment variables, Bash, Zsh, Fish shell