The value of an environment variable can contain line breaks. In /proc/PID/environ
, variables are separated by null bytes, which cannot appear in the value or in the name of an environment variable. (This is, not coincidentally, how the environment is represented in memory.) When diff sees null bytes, it decides that the file is binary (which is true by definition: text files don't contain null bytes) and gives up on displaying differences, because for most binary formats what diff would print out is not useful.
You can tell diff to go ahead and treat the files as text with diff --text
, but if you do this between two /proc/PID/environ
files, this is likely to give you a display consisting of just one or two big changed lines, because in practice the environment doesn't contain many line breaks. Diff only works with lines that are separated with a newline character. And if you do this between the content of /proc/PID/environ
and the output of printenv
, this will say that everything has changed, because printenv
uses newlines as the separator.
To get useful output, translate the null bytes into line breaks. This way each environment variable will start at the beginning of a line.
diff -u orienv --label=currenv <(tr '\0' '\n' <currenv)
To get useful and unambiguous output, also translate line breaks into null bytes, so that each environment variable is on its own single line. Then, if there's a null byte in the diff output, it indicates that the original file contains a newline at this position.
diff -u --text --label=currenv <(tr '\0\n' '\n\0' <currenv1) --label=currenv <(tr '\0\n' '\n\0' <currenv2)