Does the following show the default environment variable in a bash shell without running any startup file?

$ env -i bash --noprofile --norc
$ export
declare -x OLDPWD
declare -x PWD="/home/t"
declare -x SHLVL="1"

(Note that set will give a much longer list of default shell variables which might or might not be environment ones.)

When and how were the three environment variables created and exported?

Who created them, bash itself, or some implicit startup files which I didn't exclude when starting bash?

My question is inspired by What are the environment variables by default?


  • They are created by bash itself. – Isaac Apr 22 '18 at 0:59

When I'm curious what an open source program is doing, I'll look at the source code (and documentation, and experimentation). Looks like you've found a pretty good list, based on searching for set_auto_export ( in bash's source code for variables.c

These are inside initialize_shell_variables(), which is called from shell_initialize() in shell.c, which itself is called from main() in shell.c.

You can even find evidence of $PATH and $TERM being marked for export at one point in time, but now ifdef'd out.

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.