Environment variables are dynamic values that affect processes and programs in Linux. This guide shows you how to view, set, and manage environment variables in Bash.
Follow the guide:
View Current Environment Variables
- To display all environment variables:
Printenv
- To view a specific variable:
echo $PATH
- To list all variables (including shell variables):
Set
Set Temporary Environment Variables
- These variables exist only for the current session:
export VARIABLE_NAME="value"
- Example:
export MY_VAR="Hello World" echo $MY_VAR
- To add a directory to PATH temporarily:
export PATH=$PATH:/new/directory
Set Permanent Environment Variables
- For Current User Only
- Edit ~/.bashrc (or ~/.bash_profile on some systems):
nano ~/.bashrc
- Add at the end of the file:
export VARIABLE_NAME="value" export PATH=$PATH:/new/directory
- Save and apply changes:
source ~/.bashrc
- Edit ~/.bashrc (or ~/.bash_profile on some systems):
- For All Users (Requires Root)
- Edit /etc/environment:
sudo nano /etc/environment
- Add variables in this format (no export needed):
VARIABLE_NAME="value"
- Edit /etc/environment:
Unset Environment Variables
- To remove a variable from the current session:
unset VARIABLE_NAME
- Example:
unset MY_VAR
Verify Your Changes
- After setting a variable, verify it:
echo $VARIABLE_NAME
- Or check if it appears in the environment:
printenv | grep VARIABLE_NAME
Important Notes
- No spaces around the = sign when setting variables.
- Always use $ to reference a variable’s value.
- Use quotes for values containing spaces.
- Changes to .bashrc require a new terminal session or source command.
- System-wide changes may require a logout/login.
A Linux VPS Hosting solution gives you full control over environment variables, application settings and server configurations for greater flexibility and efficiency.
That is it! Hope you liked our article. If you encounter any issues, feel free to contact our support staff.