How to store / load exported environment variables to / from a file

Nonyme

I would like to be able to save my current environment in a file (for a running interactive session), so that I can:

  • Save it, export/modify/delete variables at will in the running session, then restore the saved environment
  • Switch at will between multiple environment
  • Detect differences between two environment

I am only interested in exported variables. As I want to be able to restore the environment it have to be a shell function, I am using bash. Ideally, it would not depends on external programs, and would work on versions of bash from v3.2.25 to current.

For now, to save my environment I use the following function:

env_save () {
    export -p > "$STORAGE/$1.sh"
}

That I use as env_save <filename> in a running session. I have some boilerplate code to keep backups, but let's ignore that.

However, I then have difficulties with loading the environment back:

env_restore () {
    source "$STORAGE/$1.sh"
}

As this would not remove spurious variables that I created in the mean time. That is, calling export -p after env_restore <filename> might not give the same output than cat $STORAGE/$1.sh.

Is there a clean way to handle that problem? I will probably need to blacklist some variables such as PWD, OLDPWD, SHELL, SHLVL, USER, SSH_*, STORAGE, etc... That is, those variable should not be saved and should not be changed when restoring as they are special variables. I cannot use a whitelist as I do not know what variables will be there.

Stéphane Chazelas

POSIXly, you can do:

# save
export -p > saved-env

...

# restore
blacklisted () {
  case $1 in
    PWD|OLDPWD|SHELL|STORAGE|-*) return 0 ;;
    *) return 1 ;;
  esac
}

eval '
  export() {
    blacklisted "${1%%=*}" || unset -v "${1%%=*}"
  }
  '"$(export -p)"
export() {
  blacklisted "${1%%=*}" || command export "$@"
}
. saved-env
unset -f export

Note that for bash not invoked as sh, you'd need to issue a set -o posix for that to work properly. Also with bash versions prior to 4.4, sourcing the output of export -p is potentially unsafe:

$ env -i 'a;reboot;=1' /bin/bash -o posix -c 'export -p'
export OLDPWD
export PWD="/"
export SHLVL="1"
export a;reboot;

ksh93 has a similar problem. yash doesn't have that particular one, but still has problems with variable names starting with -:

$ env -i -- '-p=' yash -c 'export -p'
export '-p'=''
export OLDPWD
export PWD='/'

Also beware of potential problems if you're not in the same locale when saving and restoring the variables.

bash-4.3$ locale charmap
ISO-8859-15
bash-4.3$ export Stéphane=1
bash-4.3$ export -p > a
bash-4.3$ LC_ALL=en_GB.UTF-8 bash -c '. ./a'
./a: line 5: export: `Stéphane=1': not a valid identifier

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Exported Environment Variables vs Environment Variables

From Dev

XDocument load with environment variables

From Dev

Grails: How to store/load multiple session variables?

From Dev

How to store environment variables in a Python Flask app?

From Dev

Bash read from file and store to variables in MATLAB

From Dev

Load environment variables from a shell script

From Dev

store variables from a function in the global environment

From Dev

Load file with environment variables Jenkins Pipeline

From Dev

How can I make environment variables "exported" in a shell script stick around?

From Dev

How to store / load exported environment variables to / from a file

From Dev

How to use environment variables from .env file in django views?

From Dev

Store Excel file exported from Pandas in AWS

From Dev

how to prevent exported environment variables to be recognized/accessible by a child shell (bash)?

From Dev

How to load environment variable from a file for an upstart job?

From Dev

Exported Environment Variables vs Environment Variables

From Dev

XDocument load with environment variables

From Dev

Use env with environment variables from file

From Dev

List all environment variables, and show if they are exported or not

From Dev

Set global environment variables from a custom file

From Dev

How to load *.babylon exported from blender using JavaScript/FileApi/XHR?

From Dev

How spring load configuration file by environment?

From Dev

SETX set environment variables from a file

From Dev

How to store environment variables

From Dev

How to store variables from loop to a file

From Dev

Read from a file,split it and store it in variables

From Dev

How to load environment variables into ruby gem

From Dev

How to store input line by line from an input file into variables

From Dev

NETLOGO: Reading a csv file exported from matlab and store it as a list of lists

From Dev

Store lines from .txt file in variables

Related Related

  1. 1

    Exported Environment Variables vs Environment Variables

  2. 2

    XDocument load with environment variables

  3. 3

    Grails: How to store/load multiple session variables?

  4. 4

    How to store environment variables in a Python Flask app?

  5. 5

    Bash read from file and store to variables in MATLAB

  6. 6

    Load environment variables from a shell script

  7. 7

    store variables from a function in the global environment

  8. 8

    Load file with environment variables Jenkins Pipeline

  9. 9

    How can I make environment variables "exported" in a shell script stick around?

  10. 10

    How to store / load exported environment variables to / from a file

  11. 11

    How to use environment variables from .env file in django views?

  12. 12

    Store Excel file exported from Pandas in AWS

  13. 13

    how to prevent exported environment variables to be recognized/accessible by a child shell (bash)?

  14. 14

    How to load environment variable from a file for an upstart job?

  15. 15

    Exported Environment Variables vs Environment Variables

  16. 16

    XDocument load with environment variables

  17. 17

    Use env with environment variables from file

  18. 18

    List all environment variables, and show if they are exported or not

  19. 19

    Set global environment variables from a custom file

  20. 20

    How to load *.babylon exported from blender using JavaScript/FileApi/XHR?

  21. 21

    How spring load configuration file by environment?

  22. 22

    SETX set environment variables from a file

  23. 23

    How to store environment variables

  24. 24

    How to store variables from loop to a file

  25. 25

    Read from a file,split it and store it in variables

  26. 26

    How to load environment variables into ruby gem

  27. 27

    How to store input line by line from an input file into variables

  28. 28

    NETLOGO: Reading a csv file exported from matlab and store it as a list of lists

  29. 29

    Store lines from .txt file in variables

HotTag

Archive