Posted: . At: 8:47 PM. This was 5 years ago. Post ID: 12889
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.



Sponsored



How to print the current environment variables in the Linux shell.


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.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.