0

I found my bash shell's pid

$ echo $$
7791

I then check its environment variables by two different ways:

$ cat /proc/7791/environ | tr '\0' '\n'

from https://stackoverflow.com/a/532284/156458, and

$ env

However their outputs are not identical. For example they show different values of PATH. Why do they differ? Thanks.

6

You should read the comments below that misleading answer.

/proc/$PID/environ is not used live by the shell. It merely represents the process's initial environment when it was launched.

Anything done in the shell, including its init scripts can change those values from within the shell with no change to the proc entry you're looking at.

  • Thanks. There is comment there, which happens to be my question: for a process, which is not necessary a shell, if its environment variable change, "So, where the new updated environment will stay? Which command to show it?" – Tim May 23 '17 at 6:46
  • 1
    You can examine it in /proc/$PID/environ where $PID is the process id of the env program. But you will have to be quick, since the env process exits pretty quickly. – Johan Myréen May 23 '17 at 6:49
  • @Johan: are you answering my questions? Do you mean the process shall run the env program, and at the same time run /proc/$PID/environ where $PID is the process id of the env program? – Tim May 23 '17 at 6:52
  • My answer was perhaps a bit confusing, sorry. I took the env program in your question as an example. Generally, the "environment" of a process is just an array of string=value pair passed to the process in the envp argument. The process can do whatever it wants with the values, or ignore them. A shell usually collects the values and builds a new vector that is is passed as envp to the child process. The file /proc/$PID/environ reflects what was passed in the envpvector to the process with process id $PID. – Johan Myréen May 23 '17 at 7:05
5

/proc/7791/environ shows the original environment of the shell, as it was received by the process when it was started using the execve system call:

 int execve(const char *filename, char *const argv[], char *const envp[]);

When the shell is running, it can change its set of exported variables, for example by evaluating .bashrc. This new set is then passed in the vector envp when the envprogram is started.

  • Thanks. For a process, which is not necessary a shell, if its environment variable change, will the new updated environment be stored in some file? Which command to show it?" – Tim May 23 '17 at 6:46
  • The changes to the enviroment (using setenv(3)) are done internally in the process' address space. I don't think there is a tool to inspect the changes from some other process. – Johan Myréen May 23 '17 at 9:23
1

Whatever problem you're trying to solve, you could try if using

/proc/self

solves it. Instead of getting the shell's initial environment, a

cat /proc/self/environ

displays the environment of the cat command, which should be equal to the current environment of the shell it is executed in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.