To print a listing of all environment variables in the Linux terminal or VT, use the printenv command, this prints the environment to the terminal.
deusexmachina:~ jason$ printenv TERM_PROGRAM=Apple_Terminal SHELL=/bin/bash TERM=xterm-256color TMPDIR=/var/folders/8n/777bpxj118j6690m3s7lypqw0000gn/T/ Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.zKtQPi5TVv/Render TERM_PROGRAM_VERSION=421.1 TERM_SESSION_ID=6D73ABAB-FA6D-4AEF-9885-768BC4D18F9B USER=jason SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.19fMX85EE3/Listeners PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/Applications/Wireshark.app/Contents/MacOS PWD=/Users/jason LANG=en_AU.UTF-8 XPC_FLAGS=0x0 XPC_SERVICE_NAME=0 SHLVL=1 HOME=/Users/jason LOGNAME=jason _=/usr/bin/printenv |
Get just one entry value like this. This is how to get the username.
deusexmachina:~ jason$ printenv | grep LOGNAME | awk -F '=' '{print $2}' jason |
This uses awk to get the value after the “=” delimiter.
Another example.
deusexmachina:~ jason$ printenv | grep SHELL | awk -F '=' '{print $2}' /bin/bash |
also, the env command will print out environment variables. So we can use the same trick.
4.4 Thu Jan 31 jason@Yog-Sothoth 2: $ env | grep LOGNAME | awk -F '=' '{print $2}' jason |
The set command also prints out comprehensive environment information, so it can be used with awk to get just a variable value easily.
4.4 Thu Jan 31 jason@Yog-Sothoth 0: $ set | grep SHELLOPTS | awk -F '=' '{print $2}' braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor |
These tips show how easy it is to get the value of an environment variable on Linux. There are a few ways to get environment information.