Unix & Linux
bash shell environment-variables dash
Updated Thu, 19 May 2022 23:58:50 GMT

How do I reliably convert /proc/pid/environ to arguments for the env command?

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)?


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|