144

As far as I can tell, variable assignment is the same whether it is or is not preceded by "export". What's it for?

John Bachir
  • 22,495
  • 29
  • 154
  • 227
  • 1
    See this video: [Export Variables - Why you need to Export BASH Variables](https://www.youtube.com/watch?v=xP2vbb46IyM&list=LLoLPnFiRd28Ue0-dmL_XKwA&index=3&t=0s) – mfaani Mar 20 '20 at 16:45

3 Answers3

162

Exported variables such as $HOME and $PATH are available to (inherited by) other programs run by the shell that exports them (and the programs run by those other programs, and so on) as environment variables. Regular (non-exported) variables are not available to other programs.

$ env | grep '^variable='
$                                 # No environment variable called variable
$ variable=Hello                  # Create local (non-exported) variable with value
$ env | grep '^variable='
$                                 # Still no environment variable called variable
$ export variable                 # Mark variable for export to child processes
$ env | grep '^variable='
variable=Hello
$
$ export other_variable=Goodbye   # create and initialize exported variable
$ env | grep '^other_variable='
other_variable=Goodbye
$

For more information, see the entry for the export builtin in the GNU Bash manual, and also the sections on command execution environment and environment.

Note that non-exported variables will be available to subshells run via ( ... ) and similar notations because those subshells are direct clones of the main shell:

$ othervar=present
$ (echo $othervar; echo $variable; variable=elephant; echo $variable)
present
Hello
elephant
$ echo $variable
Hello
$

The subshell can change its own copy of any variable, exported or not, and may affect the values seen by the processes it runs, but the subshell's changes cannot affect the variable in the parent shell, of course.

Some information about subshells can be found under command grouping and command execution environment in the Bash manual.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I added some new lines and also lines just showing `$` to show more clearly that there is no output from the `grep` command. Of course, feel free to rollback if you think this loses readability – fedorqui Apr 26 '15 at 18:53
  • 1
    _Note that non-exported variables will be available to subshells_ Yes - because they are copied for use by the subshell. And thus any modification in the subshell will have no effect as the copy is dropped on return-from-subshell: [Subshells, environment variables, and scope](https://en.wikibooks.org/wiki/Bash_Shell_Scripting#Subshells,_environment_variables,_and_scope). The same happens for environment variables: the are copied - but for any kind of subprocess, not only for subshells. So a `mysql` process get value from `MYSQL_PWD` env var, but would have no concept of `MYSQL_PWD` shell var. – David Tonhofer Dec 24 '18 at 15:43
  • 1
    "available to other programs" == by default available to _subprocesses of the current process only_. Because security. NOT to any other processes. Unless they have permission to inspect the process with said environment (e.g. debuggers running with the same user, or as _root_. For example, as _root_ you can read the environment of process #1: `cat /proc/1/environ` gives `HOME=/TERM=linuxBOOT_IMAGE=/vmlinuz-4.19.9-300.fc29.x86_64LANG=en_GB.UTF-8` or similar. – David Tonhofer Dec 24 '18 at 15:46
66

it makes the assignment visible to subprocesses.

$ foo=bar
$ bash -c 'echo $foo'

$ export foo
$ bash -c 'echo $foo'
bar
John Bachir
  • 22,495
  • 29
  • 154
  • 227
jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
21

Well, it generally depends on the shell. For bash, it marks the variable as "exportable" meaning that it will show up in the environment for any child processes you run.

Non-exported variables are only visible from the current process (the shell).

From the bash man page:

export [-fn] [name[=word]] ...
export -p

The supplied names are marked for automatic export to the environment of subsequently executed commands.

If the -f option is given, the names refer to functions. If no names are given, or if the -p option is supplied, a list of all names that are exported in this shell is printed.

The -n option causes the export property to be removed from each name.

If a variable name is followed by =word, the value of the variable is set to word.

export returns an exit status of 0 unless an invalid option is encountered, one of the names is not a valid shell variable name, or -f is supplied with a name that is not a function.

You can also set variables as exportable with the typeset command and automatically mark all future variable creations or modifications as such, with set -a.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953