Environment variables (also called global variables), are variables that are available in all shells that Bash uses when performing tasks and interpreting commands. They are automatically recreated by the system when a new shell is opened.
Some examples of environment variables are the PATH
, HOME
, HISTSIZE
and SHELL
variables.
Below is an example of how to display the value of a variable, using two different commands:
testuser1@testuser1-host:~$ echo $HISTSIZE 1000 testuser1@testuser1-host:~$ printenv SHELL /bin/bash
The env
command, when run without arguments, outputs a list of the environment variables. Below, you can see a sample output of the env
command:
testuser1@testuser1-host:~$ env SHELL=/bin/bash SESSION_MANAGER=local/testuser1-host:@/tmp/.ICE-unix/2625,unix/testuser1-host:/tmp/.ICE-unix/2625 QT_ACCESSIBILITY=1 COLORTERM=truecolor
You can also use the printenv
command, and you will get the same result. Below, you can see a sample output of this command:
testuser1@testuser1-host:~$ printenv SHELL=/bin/bash SESSION_MANAGER=local/testuser1-host:@/tmp/.ICE-unix/2625,unix/testuser1-host:/tmp/.ICE-unix/2625 QT_ACCESSIBILITY=1 COLORTERM=truecolor
The output of the env
or printenv
command is quite long. In the following example, you can see how to use a text search to filter that output, when you are looking for a specific variable:
testuser1@testuser1-host:~$ env | grep DISPLAY DISPLAY=:0
The output of the env
command is passed to the grep
command by the pipe | character. The grep
command search in the output for the argument (DISPLAY
, in this case).