
The env and printenv commands can be used to list the current environment variables.
~]$ env
SHELL=/bin/bash
HISTCONTROL=ignoredups
HISTSIZE=5000
HOSTNAME=DLPRWEB011
JAVA_HOME=/usr/bin/java
echo can be used to print the value of an environment variable.
~]$ echo $SHELL
/bin/bash
Some environment variables are set in the /etc/profile file, such as HISTSIZE.
~]# cat /etc/profile
HISTSIZE=1000
Some environment variables are set in files in the /etc/profile.d directory, such as LANG.
~]# cat /etc/profile.d/lang.sh
LANG=en_US.UTF-8
~]$ env | grep LANG
LANG=en_US.UTF-8
You may also have user specific environment variables in your users /home/username/.bash_profile file.
~]$ cat .bash_profile
export JAVA_HOME=/usr/bin/java
~]$ env | grep JAVA_HOME
JAVA_HOME=/usr/bin/java
export can be used to temporarily create a variable.
~]$ export FOO=bar
~]$ echo $FOO
bar
unset can be used to undefine a variable in your current shell session. Once you exit your shell session, your next shell session will only have the default environment variables.
unset FOO
It is noteworthy that /etc/security/pam_env.conf file can be used to override environment variables. For example, let's say you have the following XDG environment variables.
~]$ env | grep XDG
XDG_SESSION_TYPE=tty
XDG_SESSION_CLASS=user
XDG_SESSION_ID=3651
XDG_RUNTIME_DIR=/run/user/1002
You could add the following to /etc/security/pam_env.conf.
XDG_SESSION_TYPE DEFAULT=""
And then the XDG_SESSION_TYPE should be empty.
~]$ env | grep XDG
XDG_SESSION_TYPE=
XDG_SESSION_CLASS=user
XDG_SESSION_ID=3651
XDG_RUNTIME_DIR=/run/user/1002
Did you find this article helpful?
If so, consider buying me a coffee over at