This tutorial explains Linux Environment variables in detail. Learn how to set, reset, add, remove, create and delete environment variables locally and globally in Linux with practical examples. Environmental variables are used to customize the Linux for better user experience.
What is a linux environment variable
Linux environment variable is an object that contains value. In simple terms it is a pair of data object and their respective values. If you are familiar with programming language than you can easily understand it. Linux environment variables do same job which variables do in programming language.
If you are not familiar with programming language you can understand linux variable as a container with name which keeps value inside it. This value could be location of all executable files in the filesystem, the default editor that should be used, or the system locale settings.
Example of linux environmental variable
Let take a simple example of ls command to understand linux environmental variables. ls the basic command to list the content of directory. When execute a command in linux, you need to type the full path of that command. Since the ls command is in the /bin directory, users should execute the /bin/ls command to list files in the current directory.
Here comes the magic of linux environmental variables. Linux have a PATH variable. With the help of the PATH variable, full path is not required. The bash shell automatically searches through the directories listed in a user's PATH variable for the command that user just typed at the command line. When a matching command found shell run it. In this way environment variable provides a simple way to share configuration settings between multiple applications and processes in Linux.
How to show linux environment variable
printenv or env command can be use to list linux environment variables. The coreutils package contains printenv and env. Use printenv command to show linux environmental variables.
$ printenv
The env utility can also be used to show linux environment variables.
$env
printenv to print the names and the values of each. Note that some environment variables are user-specific. Output of env and printenv are too big to fit in screen. We can redirect the output of printenv in a file. To redirect the output of printenv in a file run following command
$printenv > tmp_file
now we can read the tmp_file with less command.
$less tmp_file
Use up arrow and down arrow key to scroll. Press q to exist from file
After reading you can simple remove the temporary file
$rm tmp_file
How to set linux environment variable
Linux environment can be set in three ways.
- Temporary also know as Session Specific Variables
- permanent locally
- Permanent globally
To set linux environmental variable temporary use export command.
PATH variable contains location of executable files. To check current PATH use following command
$ echo $PATH
Now we would add our directory in this path. Make a directory
$makdir custom_script
To add this directory in path run following commands
$export PATH="${PATH}:/home/user1/custom_script"
Verify that we have successfully added our custom_script directory in PATH variable
Now move in our custom_script directory and make a sample script
make sample_script executable and run script directly from command prompt.
You can also verify that shell run sample_script from our custom_script directory by which command
Temporary variable only available in current session. To test it log out from current user and login back.
Run sample_script again. This time you will get command not found error.
How to set linux environmental variable permanently
Defining Variables Locally
As you seen temporary variables are available only on that session. We can make those variables permanent. For security reason you should not define an environment variable globally unless you have sound understanding of linux system. For instance, you might want to add /home/user_name/custom_script to the PATH variable for a particular user. In such a case define it locally. As you do not want all other users on your system to have that in their PATH too.
The following files should be used for local environment variables on your system: ~/. profile, ~/.bash_profile, ~/.bash_login and ~/.bash_logout.
To add our custom_script directory in to the PATH variable for local usage
open .bash_profile file
vi ~/.bash_profile
add our directory /home/user1/custom_script in PATH variable
To update the variable, re-login required.
Logout
Login back
Now check that our custom path is available
Permanently set linux environmental variable Globally
root privilege requires to set linux environment variable globally. RHEL maintain and manage the environment variables in numerous files. But you do not need to pay attentions on all files that can contain environment variables. Following the RHEL recommendation you should only set environmental variables in some particular files. The following files should be used for defining global environment variables on your system: /etc/profile, /etc/bash.bashrc and /etc/environment.
/etc/profile.d Directory is used to define global script.
Login from root and move to /etc
move to /etc/profile.d directory
Make a simple test script and make this script executable
This script print a simple welcome message for all users
To test it logout from root and login back from normal user
We have successfully added global script. After testing to remove it login back from root
remove our test script
List of Linux environmental variable
Variable name | Stored information |
---|---|
DISPLAY | used by the X Window system to identify the display server |
DOMAIN | domain name |
EDITOR | stores your favorite line editor |
HISTSIZE | size of the shell history file in number of lines |
HOME | path to your home directory |
HOSTNAME | local host name |
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 |
location of your incoming mail folder | |
MANPATH | paths to search for man pages |
OS | string describing the operating system |
OSTYPE | more information about version etc. |
PAGER | used by programs like man which need to know what to do in case output is more than one terminal window. |
PATH | search paths for commands |
PS1 | primary prompt |
PS2 | secondary prompt |
PWD | present working directory |
SHELL | current shell |
TERM | terminal type |
UID | user ID |
USER(NAME) | user name |
VISUAL | your favorite full-screen editor |
XENVIRONMENT | location of your personal settings for X behavior |
XFILESEARCHPATH | paths to search for graphical libraries |