How to set environment variables in linux

To set an environment variable in Linux, you can use the export command. Here’s how you can do it:

To set a temporary environment variable for the current session:

run the following command in the terminal:

export VARIABLE_NAME=value 

Replace VARIABLE_NAME with the name of the variable you want to set, and value with the desired value. For example, to set a variable named MY_VAR with the value hello, you would run:

export MY_VAR=hello

To make the environment variable persistent across sessions:

you can add the export command to your shell’s configuration file. The configuration file varies depending on the shell you are using:

For Bash, modify the ~/.bashrc file:

echo 'export VARIABLE_NAME=value' >> ~/.bashrc

For Zsh, modify the ~/.zshrc file:

echo 'export VARIABLE_NAME=value' >> ~/.zshrc

For Fish, modify the ~/.config/fish/ file:

echo 'set -x VARIABLE_NAME value' >> ~/.config/fish/

After modifying the shell’s configuration file, you need to reload it to apply the changes. You can either open a new terminal session or run the following command to reload the configuration:

For Bash:

source ~/.bashrc

For Zsh:

source ~/.zshrc

For Fish:

source ~/.config/fish/

To verify that the environment variable is set, you can use the echo command:bashCopy codeecho $VARIABLE_NAME This will display the value of the environment variable on the terminal.

By setting environment variables, you can define custom values that can be accessed by programs and scripts running on your Linux system.

Thanks for Reading…