0

This is the shell's PID:

nathan@guixlaptop ~ $ $$
bash: 10984: command not found

I export an environment variable:

nathan@guixlaptop ~ $ export TESTVAR=test

The variable is not found in the shell's environment:

nathan@guixlaptop ~ $ cat /proc/10984/environ | grep TESTVAR || echo "fail"
fail

How do I make the variable appear in environ?

  • 2
    What is the purpose of your question? Is there a reason you wanted to see this variable in /proc/PID/environ? – aviro Jan 10 '22 at 08:02

3 Answers3

8

/proc/.../environ contains the process’ initial environment:

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

... If, after an execve(2), the process modifies its environment (e.g., by calling functions such as putenv(3) or modifying the environ(7) variable directly), this file will not reflect those changes.

To see changes, you need to start a new shell:

$ TESTVAR=test bash
$ grep TESTVAR /proc/$$/environ
grep: /proc/1825425/environ: binary file matches

You can’t change the contents shown in a running shell’s environ, unless you resort to invasive maneuvers; see change /proc/PID/environ after process start for details.

Stephen Kitt
  • 434,908
3

I think you'll need to deal with the NUL-separated strings, and restart the shell to see the result you're looking for:

$ $$
bash: 9166: command not found 
$ export TESTVAR=test_question

at this point, you can check /proc/9166/environ, but TESTVAR won't show up until the shell is re-started

$ tr '\0' '\n' < /proc/9166/environ SHELL=/bin/bash NO_AT_BRIDGE=1 PWD=/home/pi LOGNAME=pi

... etc, etc - the env var TESTVAR is not there...

$ tr '\0' '\n' < /proc/9166/environ | grep TESTVAR $

re-start bash & try again:

$ exec bash

$ tr '\0' '\n' < /proc/9166/environ | grep TESTVAR TESTVAR=test_question $

Attribution to @aviro & @stephenkitt for the simplifications (see comments)

Seamus
  • 2,925
  • 1
    You're making it more complicated than it should be. cat /proc/PID/environ | tr '\0' '\n' is much easier. – aviro Jan 10 '22 at 08:46
  • @aviro: Thanks - I saw your comment to the Q; no doubt tr is more efficient, but for me awk was easier 'cos I don't use tr often. – Seamus Jan 10 '22 at 08:53
  • 1
    You don’t even need cat (or less — take care with lesspipe & co. if you’re used to piping less into other programs): tr '\0' '\n' < /proc/PID/environ works just as well. – Stephen Kitt Jan 10 '22 at 09:20
  • @StephenKitt: You're correct & thanks for the improvement! I used less b/c I wanted to see it myself before posting an answer. – Seamus Jan 10 '22 at 09:45
0

Another examples,you can use strings show proc/$$/environ file.

strings /proc/$$/environ  # $$ mean current process PID,
                            # strings process newline characters in file.
strings /proc/$$/environ | grep USER
export TESTVAR=123
env |grep TSETVAR  # found
strings /proc/$$/environ | grep TESTVAR # not found

new session

bash strings /proc/$$/environ | grep TESTVAR # found