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


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[@]}"
|SSH_CONNECTION= 33066 22|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.