Linux Environment Variables List, Set, Create & Remove

Environment variables enhance and standardize shell environment on Linux systems. Standard environment variables are variables that the system sets up. Custom environment variables are variables that we set up for our specific requirements.

Viewing the value of environment variables

The echo command displays the specified text on the prompt. If you provide an environment variable, it shows the value of the specified environment variable. A dollar sign with the variable name instructs the shell to interpret the given text as the variable. Without it, it interprets the given text as regular and displays it as it is. Let us take an example. The SHLVL environment variable saves the levels of the shell. If we use it without the echo command with the $ sign, the echo command prints it as it is.

$echo SHLVL
SHLVL

To view the value of the SHLVL environment variable, use $ with it.

$echo $SHLVL
1

This variable keeps the number of subshells we open. The default is 1. For example, if we open three subshells, it shows the value 4 (1+3). The bash command opens a subshell. Enter this command three times and view the value of this variable again.

$bash
$bash
$bash
$echo $SHLVL
4

viewing variable

The echo command displays the value of a single variable. It requires the variable name. If you do not know the variable name or want to display a list of all environmental variables, use the env or printenv command. Both commands list all environment variables of the user who runs it. They show environment variables on the left and their values on the right. All default environment variables are in caps letters.

$env
env command
$printenv

printenv command

Creating/updating environmental variables (temporarily)

To create a new variable for the current session, specify its name and value as a command on the shell prompt. For example, the following commands define and verify an environmental variable XYZ with the value test.

$XYZ=test
$echo $XYZ
test

Use the same method to update or append an existing variable. For example, the PATH variable contains the search path for executing commands and scripts. It uses a colon (:) as the separator. The following command prints the current value of it.

$echo $PATH

verifying path variable

The following command appends the current path and adds the /opt/bin directory to the search list.

$PATH=$PATH:/opt/bin

adding temporary path

This change is temporary. It applies only to the current session. Shell removes it when the user logs out.

Creating/updating environmental variables (permanently)

A user's home directory contains the ~/.bash_profile file. It is a script file. It holds all custom environmental variables. Shell executes it every time the user logs in. Add your custom environmental variable at the end of this file. You also need to export the added environmental variable using the export command. Let us take an example. Open the ~/.bash_profile file. Add the following lines at the end of the file.

HISTSIZE=50
PATH=$PATH:/opt/bin
export HISTSIZE PATH

Save the file and log out to terminate the current session. Log in again and check the value of the HISTSIZE and PATH variables.

adding custom variables permanently

Creating/updating environmental variables for all users

Linux has a default profile file for all users. It runs this file every time a user logs in to provide a default user profile. This file is the /etc/profile. It is part of the default installation package. If an updated version of this file is available, the update process overwrites the existing file with the updated version. Because of this, administrators do not use this file to add custom environmental variables. Instead of editing this file, they create a custom script in the /etc/profile.d/ directory and use it to deploy to custom environmental variables. Shell gives priority to the variables set in the /etc/profile.d/ directory over the defaults set in the /etc/profile file. This override feature allows administrators to set custom environmental variables without worrying about updates.

environmental variable overide

Let us take an example. Login from the root user and create a file custom.sh in the /etc/profile.d/ directory. Add a new custom environmental variable. Save the file and log out from the root user account.

adding custom variable for all users

Login with a regular user account and verify the custom environmental variable.

verify custom environmental variables

Changing default or creating new environment variables only for new accounts

The /etc/skel directory contains default user profile files. When we add a new user account, the shell copies all files from this directory to the user's home directory. If we change profile files in this directory or add a new profile file, new user accounts get the updated files with the newly added custom profile file. Let us take an example. Login from the root user account. Create a backup of the default .bash_profile file and open it for editing. Add a custom environmental variable. Save the file. Add a new user account and logout from the root account.

adding custom variable

Login from the new user account and verify the custom environmental variable. It does not affect existing user accounts. To verify it, log in with an account created before this change and check it.

verifying custom variable

This tutorial is part of the tutorial series 'User profile files, environmental variables, and aliases in Linux'. Other parts of this series are the following.

Chapter 01:-  Linux User Profile Management and Environment Variable
Chapter 02:-  Linux Environment Variables List, Set, Create & Remove
Chapter 03:-  Linux alias List, Set, Create & Remove with alias Command

Default environmental variables

Name Default value
BASH  Full pathname of the BASH command
BASH_VERSION  Current BASH version number
DISPLAY  Used by the X Window system to identify the display server
DOMAIN  Domain name
EDITOR  Default command line editor
EUID  Effective user ID (EUID, numeric)
GROUPS  User's groups
HISTCMD  Current command's number in the history list
HISTFILE  The pathname of the history file
HISTFILESIZE  The size of the history file in lines
HISTSIZE  Number of commands allowed for history
HOME  Pathname for the user's home directory
HOSTFILE  Name of the hosts file if other than /etc/hosts
HOSTNAME  Hostname
HOSTTYPE  Linux platforms, such as i686, x86_64
INPUTRC  location of definition file for input devices such as keyboard
LANG  preferred language
LD_LIBRARY_PATH  paths to search for libraries
LOGNAME  Login name
MAIL  Name of specific mail file if MAILPATH is not set
MAILCHECK  Interval for checking for received mail
MAILPATH  List of mail files to be checked  for received messages
MANPATH  Paths to search for man pages
OLDPWD  Previous working directory
OS  String describing the operating system
OSTYPE  More information about the version
PAGER  Action when output is more than one terminal window
PATH  List of directories containing executable script files of commands
PPID  Process ID of the parent shell
PROMPT_COMMAND  Command to be executed before each prompt, combining the result as part of the prompt
PS1  Primary shell prompt
PS2  Secondary shell prompt
PWD  Present working directory
RANDOM     A random number
SHELL  Name of the current shell in use
SHLVL  Current shell level 
TERM  Terminal type
TMOUT  Time that the shell remains active awaiting input
UID  User ID 
USER  Username
VISUAL  Default full-screen editor
XFILESEARCHPATH  paths to search for graphical libraries

Conclusion

Environmental variables define the shell environment. You can create new or update existing ones to customize the shell environment. The /etc/profile file defines the default environmental variables for all users. The /etc/profile.d/* files override the default environmental variables for all users. The ~/.bash_profile file overrides the default environmental variables for the particular user.

ComputerNetworkingNotes Linux Tutorials Linux Environment Variables List, Set, Create & Remove

We do not accept any kind of Guest Post. Except Guest post submission, for any other query (such as adverting opportunity, product advertisement, feedback, suggestion, error reporting and technical issue) or simply just say to hello mail us ComputerNetworkingNotes@gmail.com