Do you know how to set and unset environment variables in Linux? Well, when you start a shell session in Linux, the system checks configuration files and adjusts the environment as needed. Environment variables are a sort of variable that is defined in the shell and is used during the execution of a program. They’re frequently employed in shell programs to conduct a variety of tasks.This tutorial is going to show you how to set and unset environment variable in Linux.
How to set and unset environment variables in Linux
Here are some common environment variables in Linux
- HOSTNAME – it will display the host name
- PWD – it prints the current working directory
- HOME – it’s the user home directory
- EDITOR – it displays the default text editor
- UID – it shows the unique identifier of the user
- MAIL – it’s users mail directory
- SHELL – it’s the shell used e.g. bash, sh, zsh etc.
How to check environment variables
To see all environment variables, use the printenv
command. Because there are so many variables on the list, control the view with the less
command:
printenv | less
The output will display the first page of the list, with the option to continue by pressing Space to view the next page or Enter to view the next line. Press Q to exit the display.
SHELL=/bin/bash SESSION_MANAGER=local/ubuntu:@/tmp/.ICE-unix/1651,unix/ubuntu:/tmp/.ICE-unix/1651 QT_ACCESSIBILITY=1 COLORTERM=truecolor XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg XDG_MENU_PREFIX=gnome- GNOME_DESKTOP_SESSION_ID=this-is-deprecated GTK_IM_MODULE=ibus QT4_IM_MODULE=ibus GNOME_SHELL_SESSION_MODE=ubuntu SSH_AUTH_SOCK=/run/user/1000/keyring/ssh XMODIFIERS=@im=ibus DESKTOP_SESSION=ubuntu SSH_AGENT_PID=1542 GTK_MODULES=gail:atk-bridge PWD=/home/kigz XDG_SESSION_DESKTOP=ubuntu LOGNAME=kigz XDG_SESSION_TYPE=x11 GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1 XAUTHORITY=/run/user/1000/gdm/Xauthority GJS_DEBUG_TOPICS=JS ERROR;JS LOG WINDOWPATH=2 HOME=/home/kigz USERNAME=kigz IM_CONFIG_PHASE=1 :
To look for a single variable in the environment, run the following command
printenv VARIABLE_NAME
Example, to print the home directory for current user;
printenv HOME
/home/kigz
Alternatively, you can use the echo command to display variables. e.g.
echo $[VARIABLE_NAME]
i.e.
echo $HOME
Use the grep command to locate all variables that contain a specific character string.
printenv | grep [VARIABLE_NAME]
Results for search output for the USER variable is shown below.
printenv | grep USER
USERNAME=kigz
USER=kigz
set is another command that may be used to examine environment variables. Local variables, as well as shell variables and shell functions, will be included in this command.
set | grep [VARIABLE_NAME]
Using the command line, the simplest way to set a variable is to input its name followed by a value.
[VARIABLE_NAME]=[variable_value]
Create a variable called DEMO with a text value as an example. The shell does not provide any output if you type the command correctly.
DEMO="This is an example variable"
The command set | grep
verifies that the variable was created. Printenv
, on the other hand, produces nothing because the variable created this way is a shell variable, this is the case.
set | grep DEMO
DEMO='This is an example variable'
printenv DEMO
This is an example variable
How to Export an Environment Variable
Return to the parent shell and export the variable with the export command if you wish to make it an environment variable.
export [VARIABLE_NAME]
To verify that the export was successful, use printenv
export DEMO
printenv DEMO
This is an example variable
If you run echo in a child shell session right now, it will return the value of the environment variable.
bash
echo $DEMO
This is an example variable
After you end the current shell session, the environment variable you generated this way vanishes.
Permanently set an environment variable in Linux
You must set an environmental variable as a permanent variable if you want a variable to persist after you stop the shell session.
You can specify whether it applies to the current user or all users.
Set User Environment Variables on ~/.bashrc
Edit the .bashrc
file to set persistent environment variables for a single user.
sudo nano ~/.bashrc
Use the following syntax to create a line for each variable you want to add.
export [VARIABLE_NAME]=[variable_value]
See example to set DEMO environment variable.
# ~/.bashrc: executed by bash(1) for non-login shells. # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) # for examples # If not running interactively, don't do anything case $- in *i*) ;; *) return;; esac export DEMO="This is an example variable" # don't put duplicate lines or lines starting with space in the history. # See bash(1) for more options HISTCONTROL=ignoreboth # append to the history file, don't overwrite it shopt -s histappend
Save and close the file.
Close and open the terminal. The changes will take effect.
Set Systemd Wide Environment Variables on /etc/profile.d
Create an .sh
file in the /etc/profile.d
folder to set permanent environment variables for all users.
sudo nano /etc/profile.d/[filename].sh
The syntax for adding variables to the file is identical to that of the .bashrc
file.
sudo nano /etc/profile.d/demoname.sh
export DEMO="This is an example variable"
The changes will be applied when you log in again, save and exit the file.
Unset Environment Variable
Run the command below to unset an environment variable
unset [VARIABLE_NAME]
This command deletes variables permanently that were exported via a terminal command.
Variables from configuration files are removed from the current shell session as well. They are, however, reset when you log in again.
Remove the line containing the variable definition from the file to permanently unset a variable you saved.
That marks the end of our tutorial on how to set and unset environment variables in Linux.
Other Tutorials
How to list hardware information in Linux
Use Find Command to locate files older than specific time in Linux