To create a new environment variable, you can use the export command in your terminal. Here’s how to do it:
Temporary Environment Variable
This variable will only exist for the duration of the current shell session.
-
Open your terminal.
-
Use the
exportcommand to create the variable:export MY_VARIABLE="my_value" -
You can verify that it has been created by using:
echo $MY_VARIABLE
Permanent Environment Variable
To make the variable persistent across sessions, you need to add it to your shell's configuration file.
For Bash:
- Open the
.bashrcfile in your home directory:nano ~/.bashrc - Add the following line at the end of the file:
export MY_VARIABLE="my_value" - Save and exit the editor (in Nano, press
CTRL + X, thenY, andEnter). - To apply the changes, run:
source ~/.bashrc
For Zsh:
- Open the
.zshrcfile in your home directory:nano ~/.zshrc - Add the following line at the end of the file:
export MY_VARIABLE="my_value" - Save and exit the editor.
- To apply the changes, run:
source ~/.zshrc
System-wide Environment Variable
To set an environment variable that is available to all users:
- Open or create a file in
/etc/profile.d/:sudo nano /etc/profile.d/my_variables.sh - Add the following line:
export MY_VARIABLE="my_value" - Save and exit the editor.
After following these steps, the new environment variable will be created and available in your shell sessions.
