Is it possible to invoke a bash such that everything about my environment (aliases, function, exported/non-exported variables, etc) will be present in the new shell?

I'd settle for just getting the shell variables that aren't part of my environment. I run bash --norc and the first thing I notice is I've lost $PS1 and my ls colours. I could simply export those variables before calling bash, but I'm worried about the whole slew of other things I might miss.


You might want to look into the alias and set command. If run without parameters, alias will list all defined parameters and set will list all variables and functions. Pushing the output of these commands to a temporary file and then telling bash to read this file as an rc file may accomplish what your looking for.

| improve this answer | |
  • This is the closest I've been yet, but I've finally accepted that what I really want is impossible. I want to write a Perl script that sets my environment. It would do some stuff, set some variables, and finally exec bash. I can't afford to let the environment be tainted by rc init files, but I want things like ls coloring and a descent PS1. I think maybe my best bet is to have the Perl script print the environment changes, and have the user invoke it with back ticks. – Andrew Wood Jan 27 '11 at 15:55
  • depending on the complexity of the perl script, have it write it's own temporary rc file and then invoke bash with: bash --rcfile <path-to-the-temp-rc-file> This will not load the system wide or user rc file, only the file specified. – cledoux Jan 27 '11 at 16:17
  • I was part way through doing exactly that when I realized the script won't have access to its parent's aliases and function - only its environment. – Andrew Wood Jan 27 '11 at 16:20


You can, however, enable auto-export of functions and variables by adding set -a at the top of your rc script.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.