Views: 46
0 0
Read Time:1 Minute, 30 Second

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

~$ printenv

and 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/printenv

This 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
abc

Now 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 set

PHP 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 set

Hope this helped

cc4ae2d7d0e2367495d9f75c31beef8e?s=400&d=robohash&r=g How to show all the environment variables

About Post Author

brewedbrilliance.net

Experienced software architect with a spasmodic passion for tech, software, programming. With more than 20years of experience I've decided to share all my knowledge in pills through this blog. Please feel free to contact me if there is any topic that you would love to cover
happy How to show all the environment variables
Happy
0 %
sad How to show all the environment variables
Sad
0 %
excited How to show all the environment variables
Excited
0 %
sleepy How to show all the environment variables
Sleepy
0 %
angry How to show all the environment variables
Angry
0 %
surprise How to show all the environment variables
Surprise
0 %

Share this content:

By brewedbrilliance.net

Experienced software architect with a spasmodic passion for tech, software, programming. With more than 20years of experience I've decided to share all my knowledge in pills through this blog. Please feel free to contact me if there is any topic that you would love to cover

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x