3

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.

2
  • Specifically environment variables and not just shell variables? Commented Dec 10, 2016 at 16:34
  • environment variables and not just shell variables Commented Dec 10, 2016 at 17:55

1 Answer 1

8

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 use printenv to show the environment. Some examples:

$ env -i printenv

$ env -i sh -c printenv
PWD=/home/peter
SHLVL=1
_=/usr/bin/printenv

$ echo printenv | env -i sh
PWD=/home/peter
SHLVL=1
_=/usr/bin/printenv

$ env -i sh --login -c printenv
...all kinds of variables from login scripts...
LANG=en_US.UTF-8
PWD=/home/peter
SHLVL=1
PATH=/usr/local/bin:/usr/bin:...
LESSOPEN=|/usr/bin/lesspipe.sh %s
_=/usr/bin/env

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:

$ env -i -c set
BASH=/usr/bin/sh
...
BASH_VERSION='4.4.5(1)-release'
...
SHLVL=1
TERM=dumb
UID=1000
_=sh

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:

$ declare -p PWD
declare -x PWD="/home/peter"
$ declare -p foo
bash: declare: foo: not found
$ foo=bar
$ declare -p foo
declare -- foo="bar"

The -x attribute mark a variable as exported which means that subprocesses see this variable. To do this for existing variables, you can use the export builtin:

$ export foo
$ declare -p foo
declare -x foo="bar"

In Bash, setting a variable and making it available to subprocesses can be combined:

$ export foo=bar
6
  • Thanks. How about the question 'Is it correct that some but all the builtin variables are default environment variables?' Commented Dec 10, 2016 at 19:04
  • @Tim I don't know what you mean by "default environment variable", but hope that the addition on exported variables clarifies things a bit. Commented Dec 10, 2016 at 20:39
  • I meant that if a shell builtin variable exists i.e. is set in a bash process, must it be an environment variable in the bash process? Commented Dec 10, 2016 at 20:56
  • @Tim A variable set in a bash process is not an environment variable. Counter-example: sh -c 'export foo=bar; grep foo < /proc/self/environ'. Note that the two terms you use ("builtin variable" and "default environment variable") are not defined. The bash manual puts variables like PWD in the category "Variables set by the shell". A "builtin command" on the other hand is defined (e.g., cd, pwd, export, etc.). Commented Dec 11, 2016 at 12:58
  • builtin variables gnu.org/software/bash/manual/html_node/… and gnu.org/software/bash/manual/html_node/Bash-Variables.html. Default environment variables are environment variables not set by the initialization startup file or by your calls to commands such as export. My question is that is a shell builtin variables necessarily be a default environment variable? Commented Dec 11, 2016 at 14:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.