1. Overview

It’s common practice for Linux processes to use environment variables. They allow the program to know the context they’re running in. For example, the environment variables may store information about the location of system libraries, user profile settings, or temporary file directories.

In this tutorial, we’ll learn how to check the environment variables used by a running Linux process.

2. Getting the Process ID

Before we can request the environment variables of a Linux process, we’ll need to know its PID. PID stands for process ID, which is a unique number assigned to each process.

The most common commands for getting the PID are ps and pidof.

Let’s use the pidof command to obtain the PID of our process:

# pidof processname
1475

So, our process has the PID of 1475, which we are going to use in the following sections.

3. Printing Environment Variables

In the section above, we obtained the PID number of a process. We’re now going to use that PID to print the list of its environment variables.

For that, we can print the content of a file in the /proc pseudo-filesystem. Specifically, the /proc//environ, where is the process ID number. In our case, we’re going to use the  file /proc/1475/environ.

Let’s use cat to print the contents of the environment variables file:

$ cat /proc/1475/environ --show-nonprinting
HOME=/^@TERM=linux^@PATH=/usr/sbin:/usr/bin:/sbin:/bin

Notice that we used the –show-nonprinting option in order to show the null character ^@, which separates one variable’s values from the next. We can see the HOME, TERM, and PATH variables are set for process 1475.

4. Getting a More Readable Output

Although we managed to print the list of all environment variables of our process, the output format was not very convenient to read. We can make the output more human readable by displaying each variable on a new line.

For that, we’re going to pipe the initial output to the tr command. It will allow us to parse each null character in the printed text and replace it with a new line character \n, for example:

# cat /proc/1475/environ | tr '\0' '\n'
HOME=/
TERM=linux
PATH=/usr/sbin:/usr/bin:/sbin:/bin

As we can see, now all environment variables are printed in a readable form on the new lines.

5. Using the ps Command

Another way to check the environment variables of a Linux process is to use the ps command. In particular, ps eww , where option -e selects all processes, while option -ww stands for wide output with unlimited width (sometimes the list of variables is very long). Let’s use this command for our process 1475:

$ ps eww 1475
    PID TTY      STAT   TIME COMMAND
   1475 ...       ...    ... processname HOME=/ TERM=linux PATH=/usr/sbin:/usr/bin:/sbin:/bin

In the output, the COMMAND column is our main interest because this is where the environment variables are displayed with spaces in between.

6. Conclusion

In this tutorial, we learned about ways to display the environment variables of a Linux process. Firstly, we discovered how to get the process ID. Secondly, we studied how to print the content of the */proc//*environ file using the cat command and make the output more readable. Finally, we looked at one more way of getting the environment variables using the ps command.