When starting a bash shell process, what are the default environment variables, except those specified in the startup file?
Is it correct that some bash or sh builtin variables (listed in the "Bourne Shell Variables" and "Bash Variables" sections of the Bash manual) are default environment variables while other bash or sh builtin variables are not? What kinds of bash or sh builtin variables are default environment variables?
In the POSIX definition of environment variables it seems that all or most of the builtin variables in bash are environment variables by default, but I am not sure.
Best Answer
A process normally inherits envrionment variables from its parent process. Unless programs (e.g. shells) have other conventions, there is no "default" environment variable.
If you are curious, you could use the
env -i
command to clear the environment and useprintenv
to show the environment. Some examples:The bash(1) manual documents some of these variables, but unfortunately it does not provide a definitive answer on whether these environment variables are always set or not.
Other variables in bash can be found in a similar way:
If you need to rely on any of these variables, it is best to check the bash manual. In particular:
Now given that you have an open bash shell. You would like to know if a certain variable is available to subshells or not. For this, the
declare -p NAME...
builtin can be used to "display the attributes and value of each NAME. Example:The
-x
attribute mark a variable as exported which means that subprocesses see this variable. To do this for existing variables, you can use theexport
builtin:In Bash, setting a variable and making it available to subprocesses can be combined: