2

How do I reliable convert /proc/pid/environ to argument for the env command?

  • IFS=$'\n' ; env -i $(xargs -0n1 -a /proc/$$/environ) only works when all environment variables do not have the newline character.
  • IFS=$'\0'is not working in bash and dash.

Is there any other available way (included any other shell way)?

1 Answer 1

4

In bash, you can create an array from entries in the environ file, and use the array elements as arguments to env:

mapfile -d '' envs < /proc/$$/environ
env -i "${envs[@]}" ...

It seems to work fine with newlines:

$ foo=$'a\nb' bash
$ mapfile -d '' envs < /proc/$$/environ
$ printf '|%s|\n' "${envs[@]}"
|foo=a
b|
|LC_MEASUREMENT=en_GB.UTF-8|
|SSH_CONNECTION=127.0.0.1 33066 127.0.0.1 22|
...

You must log in to answer this question.

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