You can set environment variables in various ways depending on the operating system and the shell you are using. Here are some common methods:
In Unix/Linux (Bash Shell)
-
Temporary Environment Variable (only for the current session):
export VARIABLE_NAME="value" -
Persistent Environment Variable (across sessions):
- Open your
.bashrcor.bash_profilefile:nano ~/.bashrc - Add the following line at the end of the file:
export VARIABLE_NAME="value" - Save and exit the editor, then run:
source ~/.bashrc
- Open your
In Unix/Linux (Zsh Shell)
-
Temporary Environment Variable:
export VARIABLE_NAME="value" -
Persistent Environment Variable:
- Open your
.zshrcfile:nano ~/.zshrc - Add the following line:
export VARIABLE_NAME="value" - Save and exit, then run:
source ~/.zshrc
- Open your
In Windows (Command Prompt)
-
Temporary Environment Variable:
set VARIABLE_NAME=value -
Persistent Environment Variable:
setx VARIABLE_NAME "value"
In Windows (PowerShell)
-
Temporary Environment Variable:
$env:VARIABLE_NAME="value" -
Persistent Environment Variable:
[System.Environment]::SetEnvironmentVariable("VARIABLE_NAME", "value", "User")
Example
For example, to set a variable named MY_VAR with the value HelloWorld in a Bash shell, you would do:
export MY_VAR="HelloWorld"
To make it persistent, add the same line to your .bashrc or .bash_profile file and source it.
