Tutorial :Looking to get the env vars of a PID in BASH for a BASH script


Any way to do this?

I have a script that exports a few vars and runs a component.

I'm looking to find out the actual values of some of these vars while the process is running. (Which applies here, because I'll be incorporating those values into another script/program)

The vars themselves are PID specific (I'm quite new at this, if that wasn't obvious. Haha!)



You can query /proc/PID/environ for all defined envvars


I don't sure I understand your question. but ... If you in your bash script export some environment variables, than this will be in the environment of process it is executing (this since child process derives all environment variable of process created it).


I checked the environ file for that process, but oddly, it doesn't contain all the defined env vars for that process in and of itself.

The exported variables will be specific to a process, but I won't always know them. I'm looking to query which env vars are specific and declared for a process, and what their values are.


Hmm, If I understand correctly - I think i would make the script respond to a "kill" signal so as to extract it's runtime information using "trap". You could write the metadata to stdout potentially.

Maybe trap SIGHUP in your script, and make it write to stdout?


If it's Linux you can do:

xargs -0 printf '%s\n' < /proc/${pid}/environ  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »