I'm trying to get the content of any /proc/*PID*/environ file in more readable format. I'm able to do that in the way shown below, but I'm sure this isn't the proper way.

$ cat "/proc/$(pgrep gnome-session -n -U $UID)/environ"
$ cat -e "/proc/$(pgrep gnome-session -n -U $UID)/environ"
$ cat -e "/proc/$(pgrep gnome-session -n -U $UID)/environ" | sed 's/\^@/\n/g'

Maybe I must assign a specific value to $IFS, but what is it? What is the proper way to achieve the above result?


6 Answers 6


The entries are separated by the null character, see man 5 proc:

      This file contains the environment for the process.  The entries
      are separated by null bytes ('\0'), and there may be a null byte
      at  the  end.

So a simple way is to apply xargs -0 -L1 on it:

$ xargs -0 -L1 -a /proc/self/environ
  • -0 - read null-delimited lines,
  • -L1 - read one line per execution of command
  • -a file read lines from file
  • and if no command is specified, xargs simply prints the line.

Various GNU commands have options to work with null-delimited data: -z for sed, sort, uniq, grep etc, and for filenames, -print0 with find and -Z with grep.

Alternatively, you can use plain old bash:

while IFS= read -d '' -r line
    printf "%q\n" "$line"
done < /proc/.../environ

-d '' tells read to read until a null byte, IFS= and -r prevent field-splitting and backslash-escaping, so that the data is read as-is, %q will quote special characters in output.

Since you did use sed, you could have done:

sed -z 's/$/\n/' /proc/.../environ

which just tacks on a newline at the end of each null-delimited line.

  • 1
    +1 for the default of xargs to print, I wasn't aware of that Nov 21, 2017 at 20:43
  • 2
    Keep in mind, though, that the entries are null-terminated precisely because it is the only character that cannot be part of a value. If you replace them with newlines, you potentially introduce an ambiguity of whether two consecutive lines represent two variables, or one variable whose value contains a newline followed by what looks like an assignment.
    – chepner
    Nov 21, 2017 at 20:45
  • +1 The plain old bash answer is useful for containers where you don't have a lot else. Jan 29 at 14:03
  • You could use cut:

    cut -d '' -f1- --output-delimiter=$'\n' /proc/$pid/environ

    using null as a delimiter, and outputting a newline delimiter, optionally picking only certain fields/lines.

  • Or filter through tr, translating nulls to newlines:

    tr '\0' '\n' </proc/$pid/environ
  • Or just stick with your sed version...

  • 11
    I've been using tr '\0' '\n' ever since I can recall. That's the most intuitive way for me to express "replace all NULs with newlines", and hence my personal preferred solution.
    – egmont
    Nov 21, 2017 at 19:40

You can use strings as follows:

strings /proc/$(pgrep gnome-session -n -U $UID)/environ

Sample of the output:

$ strings /proc/$(pgrep gnome-session -n -U $UID)/environ

man strings


   strings - print the strings of printable characters in files.


   For each file given, GNU strings prints the printable character
   sequences that are at least 4 characters long (or the number given with
   the options below) and are followed by an unprintable character.  By
   default, it only prints the strings from the initialized and loaded
   sections of object files; for other types of files, it prints the
   strings from the whole file.

   strings is mainly useful for determining the contents of non-text
  • 4
    that's beautiful.
    – Prisoner
    Oct 24, 2019 at 9:56
  • 1
    As written in the quoted manual, strings only prints strings of length >= 4. Environment variables like A=1 will be ignored. Also, whitespace (expect space and tab) is ignored. You can change this by using strings -n2 -w instead.
    – Socowi
    Mar 16, 2021 at 23:44
  • perfect, love it. also thanks for the -n2 thingy
    – tollo
    Mar 20 at 12:39

"man proc", search "environ", help text says:

Thus, to print out the envi‐ronment of process 1, you would do:

$ strings /proc/1/environ

Building on @muru's answer, you can then export those variables with the following:

`cat /proc/1/environ | xargs -0 -L1 -I{} echo export {}`
hexdump -v -e '/1 "%02X "' /proc/PID/environ |sed -e 's/00/0a/g' | xxd -r -p 

how does this work first hexdump -v -e '/1 "%02X "' /proc/PID/environ will (if you replace PID with the pid of the program or self) print all of the file in hex with bite separated with a space. next i pipe it into sed -e 's/00/0a/g' which takes all 00 aka nulls which are used as separators for each entry and converts them into newlines. lest i use xxd -r -p to convert back the hex to binary aka readable text.

  • 3
    Can you explain a little bit how this works and how this will solve OP's question?
    – Thomas Ward
    Oct 11, 2021 at 2:53
  • A good explaination would be really interesting. I understand that hexdump is used to make a hexadecimal representation of the environ-content which used to convert NUL-Characters into newlines and convert it back into binary. I'm personally interested why @Inee chose those tools.
    – MadMike
    Oct 22, 2021 at 8:33
  • because i use (and made) a tool called bintools (you can find it here github.com/lnee94/resh/blob/main/l/bintools ) and it has thos commands in it and it seems the most deterministic
    – lnee
    Oct 22, 2021 at 14:30

You must log in to answer this question.

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