I need to save environment variables to a file and (later) a process should import them. I need this to workaround a program fooprog that nullifies the environment before executing a barscript, and I need some environment variables in barscript. So I'm planning on saving all environment variables to a file before executing fooprogram, and make barscript source them from a file.I tried this, but it doesn't work if there are variables that contain spaces.$ env > my_environment$ . my_environmentI guess the correct way is to fiddle with /proc/$$/environ and parsing this from barscript somehow, but I can't find the correct way of doing that.edit: Quotes or newlines are problematic too. How could it be done so that envars as problematic as the following could be sourced without problems?PROBLEMATIC='this has double quotes  and single quotes '\'' and evennewlines.See?'
export (save) and import (source) shell variables
bash;environment variables
$ declare -p > my_environmentLater, inside barscript:. my_environmentdeclare -p outputs environment variables in a form that can be executed by the shell, including quoting and escaping of variables as required.$ foobar='some double-quoted text'$ echo $foobar some double-quoted text$ declare -p foobardeclare -- foobar=\some double-quoted text\BTW, if you don't want to output your entire environment, you don't have to.  Just name the vars you want to output on the declare -p command line:$ declare -p HOME PATH foobardeclare -x HOME=/home/casdeclare -x PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11declare -- foobar=\double-quoted-text\or pipe through grep -v to exclude specific vars you don't want in the output.To get only the exported variables:declare -p -x > my_environmentFor more info, see help declare in bash.