This is a small guide on how to show all the environment variables. These command will came very handy in environments where, we are retrieving values from the environment. This is very common when working with containers or pods and will be very helpful if you are trying to debug.
From the shell you can run
~$ printenvand you will see as output something like this
SHELL=/bin/bash
PWD=/home/brewedbrilliance
LOGNAME=brewedbrilliance
XDG_SESSION_TYPE=tty
MOTD_SHOWN=pam
HOME=/home/brewedbrilliance
LANG=en_US.UTF-8
LC_TERMINAL=iTerm2
LESSCLOSE=/usr/bin/lesspipe %s %s
XDG_SESSION_CLASS=user
TERM=xterm-256color
LESSOPEN=| /usr/bin/lesspipe %s
USER=brilliance
LC_TERMINAL_VERSION=3.4.8
SHLVL=1
XDG_SESSION_ID=4720
XDG_RUNTIME_DIR=/run/user/1000
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
PATH=/home/brewedbrilliance/.local/bin:/snap/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
SSH_TTY=/dev/pts/3
_=/usr/bin/printenvThis as it is, is not really useful but you can access these values programmatically. As example let’s set an environment variable and try to retrieve using python or php
~$ export TEST_VAL="abc"
~$ echo $TEST_VAL
abcNow we have our TEST_VAL set
python example:
import os
val = os.getenv("TEST_VAL", "Not set")
print(val)we can then execute
~$ python3 i.py
abc
~$ unset TEST_VAL
~$ python3 i.py
Not setPHP example
<?php
$i = getenv("TEST_VAL") != "" ? getenv("TEST_VAL"): "Not set";
echo $i . PHP_EOL;
?>we can then execute
~$ export TEST_VAL="abc"
~$ php i.php
abc
~$ unset TEST_VAL
~$ php i.php
Not setHope this helped
Share this content:
