1

I am learning about linux and I have a question that I have encountered in one of the books I read:

The following are two different ways to print out environment variables. Please describe their differences:

$ /usr/bin/env
$ /usr/bin/strings /proc/$$/environ

I searched both in Google and in the book, and everything I found suggests that both methods prints out the environment variables of the current process, but when I try to run them, I see differences but I don't know why.

using the following commands:

/usr/bin/env > file1 

/usr/bin/strings /proc/$$/environ > file2

Now I'm want to see some difference between the files, so i sort the lines and check whether a line in file1 isn't appear in file2 and vice versa

The result of comm -13 <(sort -u file1) <(sort -u file2) is

enter image description here

and when I use comm -13 <(sort -u file2) <(sort -u file1) enter image description here

(source for the command: Check whether all lines of file occur in different file)

4
  • /proc/$$/environ is null-separated: xargs -0 -L1 < /proc/$$/environ will produce the same content as env, perhaps in a different order. Apr 18, 2022 at 13:46
  • It would be helpful if you could show an example of how they are different. Apr 18, 2022 at 13:47
  • I edited an example of what I currently see in the cmd Apr 18, 2022 at 14:03
  • 4
    Please don't post images of text. Instead, copy/paste the text into your question and use the formatting tools to format it as code.
    – terdon
    Apr 18, 2022 at 14:10

1 Answer 1

1

From the proc(5) manual on a current Ubuntu system, describing /proc/[pid]/environ:

This file contains the initial environment that was set when the currently executing program was started via execve(2).

Since $$ is the PID of the current shell, /proc/$$/environ would contain the environment of the current shell, the way it look when the shell started before any of the shell's initialisation files were executed.

The env utility would output the environment the way it currently looks. This may differ if your shell's initialisation scripts modified the environment or if you have manually exported new shell variables into the environment or unset environment variables.

From the information that you show, we can see that the current working directory, PWD, was /home/seed when the shell started, but that you moved to /home/seed/Desktop/Lab1 to run the env command (by first moving to /home/seed/Desktop, as indicated by OLDPWD). We can also see that your shell's initialisation files set LESSOPEN, LESSCLOSE, and LS_COLORS and modified the PATH variable.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .