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
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 [email protected]=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
Example, to print the home directory for current user;
Alternatively, you can use the echo command to display variables. e.g.
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
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.
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"
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'
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.
To verify that the export was successful, use
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.
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
.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.
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
.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
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
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.
How to list hardware information in Linux
Use Find Command to locate files older than specific time in Linux