bashenvironment-variablesenv

env -0 dump environment. But how to load it?


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


Solution

  • 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"