The linux command line tool env
can dump the current environment.
Since there are some special characters I want to use env -0
(end each output line with 0 byte rather than newline).
But how to load this dump again?
Bash Version: 4.2.53
The linux command line tool env
can dump the current environment.
Since there are some special characters I want to use env -0
(end each output line with 0 byte rather than newline).
But how to load this dump again?
Bash Version: 4.2.53
Don't use env
; use declare -px
, which outputs the values of exported variables in a form that can be re-executed.
$ declare -px > env.sh
$ source env.sh
This also gives you the possibility of saving non-exported variables as well, which env
does not have access to: just use declare -p
(dropping the -x
option).
For example, if you wrote foo=$'hello\nworld'
, env
produces the output
foo=hello
world
while declare -px
produces the output
declare -x foo="hello
world"
If you want to load the export of env
you can use what is described in Set environment variables from file:
env > env_file
set -o allexport
source env_file
set +o allexport
But if you happen to export with -0
it uses (from man env
):
-0, --null
end each output line with 0 byte rather than newline
So you can loop through the file using 0
as the character delimiter to mark the end of the line (more description in What does IFS= do in this bash loop: cat file | while IFS= read -r line; do … done
):
env -0 > env_file
while IFS= read -r -d $'\0' var
do
export "$var"
done < env_file