Variable assignments such as the ones we've done so far in this chapter are only visible to the current running shell. Their values don't implicitly affect a program called by the script – a forked process.
The easiest way to demonstrate this on the command line is to start another shell process after making an assignment. Consider this basic shell session, including an assignment to a variable named realname:
bash$ declare -p BASHPID declare -ir BASHPID="7013" bash$ realname='Bash User' bash$ declare -p realname declare -- realname='Bash User'
If we start a new bash process, we get a new process ID in BASHPID, and the realname variable does not show as a set:
bash$ bash bash$ declare -p BASHPID declare -ir BASHPID="7585" bash$ declare -p realname bash: declare: realname: not found
Once we use exit to return to the parent process, it shows as available again:
bash$ exit bash$ declare -p BASHPID declare -ir BASHPID="7013" bash$ declare -p realname declare -- realname='Bash User'
If you want a way for variables to be visible and usable by any of the programs you call on the script or command line, you need to use environment variables. The POSIX-specified command to do this is called export, which is a builtin command in Bash:
bash$ REALNAME='Bash User' bash$ export REALNAME bash$ declare -p REALNAME declare -x REALNAME="Bash User"
Notice that the declare output for the preceding command is slightly different from our system example; it includes the -x option, specifying that the REALNAME variable has been exported to the environment. This export makes the variable available to child processes:
bash$ bash bash$ declare -p REALNAME declare -x REALNAME="Bash User" bash$ exit
You can get a list of all environment variables and their values with declare -x in Bash. The POSIX-specified env program is another option, called with no arguments, but you might want to sort its output in order to read it.
You should only export variables when you actually need to use their values in subprocesses. It may be tempting to export all of your variables, but it's untidy, and you risk overwriting an important environment variable used by a program without intending to, which can be hard to debug.