On UNIX environments in general, and on the operating systems derived from UNIX, such as Linux or BSD, variables are features that allows the shell or the user to store data. The stored data can be used to change the behavior of how the Bash shell (or other commands) work in some cases, or to provide critical system information in other cases.
Variables are identified by names and they are temporarily stored in memory. Speaking of Bash shell, we can distinguish between two types of variables: local (or shell) variables, and environment variables.
To get the names and values of all currently defined environment variables, you can use the printenv
command. 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
You can achieve the same result by using the env
command. This command can also be used for doing temporary, short-term changes to the environment, but I will not go into those details here.
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 examine the value of a particular variable, by specifying its name to the printenv
command:
testuser1@testuser1-host:~$ printenv SHELL /bin/bash
You can also achieve that by using the echo
command to print the content of a variable. You have to use the dollar sign ($
) in front of the variable's name, as in the following example:
testuser1@testuser1-host:~$ echo $SHELL /bin/bash
You can use the environment variables in many shell commands. For example, you can list the contents of the "Desktop" directory within the current user's home directory, by using the following command:
testuser1@testuser1-host:~$ ls $HOME/Desktop/
If you want to modify the value of an existing variable, use the assignment expression:
testuser1@testuser1-host:~$ echo $HISTSIZE 1000 testuser1@testuser1-host:~$ HISTSIZE=2500 testuser1@testuser1-host:~$ echo $HISTSIZE 2500