How to display process environment

In this brief article, I will tell you how to display environment variables associated with running process.

Take advantage of proc file-system to print contents of the /proc/ProcessID/environ file.

$ sudo cat /proc/1/environ | tr '\0' '\n'
HOME=/
init=/sbin/init
recovery=
TERM=linux
drop_caps=
BOOT_IMAGE=/vmlinuz-4.4.0-21-generic
PATH=/sbin:/usr/sbin:/bin:/usr/bin
PWD=/
rootmnt=/root

Use pgrep and xargs to display environment for multiple processes at the same time.

$ pgrep mint | xargs -r -I {} sh -c "echo \"* Process {}: \$(cat /proc/{}/cmdline)\"; sudo  cat /proc/{}/environ;" | tr '\0' '\n'
* Process 26898: /usr/bin/python3/usr/bin/mintupdate-launcher
USER=milosz
HOME=/home/milosz
DESKTOP_SESSION=cinnamon
USERNAME=milosz
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
SESSION_MANAGER=local/runner:@/tmp/.ICE-unix/26588,unix/runner:/tmp/.ICE-unix/26588
DISPLAY=:0
LANG=pl_PL.UTF-8
XAUTHORITY=/home/milosz/.Xauthority
SHELL=/bin/bash
PWD=/home/milosz
* Process 26904: /usr/bin/python3/usr/lib/linuxmint/mintUpdate/mintUpdate.py
USER=milosz
HOME=/home/milosz
DESKTOP_SESSION=cinnamon
USERNAME=milosz
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
SESSION_MANAGER=local/runner:@/tmp/.ICE-unix/26588,unix/runner:/tmp/.ICE-unix/26588
DISPLAY=:0
LANG=pl_PL.UTF-8
XAUTHORITY=/home/milosz/.Xauthority
SHELL=/bin/bash
PWD=/home/milosz

Just remember to replace null byte \0 as it used to separate entries.

Milosz Galazka's Picture

About Milosz Galazka

Milosz is a system administrator working for a successful Polish company and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu