Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

3
  • This may work. (especially your last part to save the env variable before the script clobbers it). The clobbering value is coming from an import into the bash script using the . <path to file> method at the very top of the script beneath the #!/bin/bash header. Maybe I don't understand importing fully, am I able to import the other script (and thus the offending variable) after using your trick to save the existing env variable? Commented Mar 24, 2014 at 16:15
  • 1
    The . command tells the shell to read and interpret the code in the given file. It's like #include in C. export -p dumps the values of the currently exported variables (the ones explicitly exported with export and the ones that were in the environment that the shell received on startup). So you'll want to run it at the point where you want the values saved. Commented Mar 25, 2014 at 15:03
  • Yet another trick for Bash would be wrapping the entire script-body in a function, and do some magic to unexport and mark the environment as local: ::() { source <(export -p | sed 's/-/+/'); script-body; }; :: "$@" Commented Sep 2, 2020 at 6:17