What is the export command supposed to do in Linux?

Export a shell variable as environment variable.


Here is an example to demonstrate the behavior.

$ # set testvar to be a value
$ testvar=asdf
$ # demonstrate that it is set in the current shell
$ echo $testvar
$ # create a bash subprocess and examine the environment.
$ bash -c "export | grep 'testvar'"

$ bash -c 'echo $testvar'

$ # export testvar and set it to the a value of foo
$ export testvar=foo
$ # create a bash subprocess and examine the environment.
$ bash -c "export | grep 'testvar'"
declare -x testvar="foo"
$ bash -c 'echo $testvar'
foo
$ # mark testvar to not be exported
$ export -n testvar
$ bash -c "export | grep 'testvar'"

$ bash -c 'echo $testvar'

You will notice that without export the new bash process you created was not able to see testvar. When testvar was exported, the new process was able to see testvar.


Please see this Bash by example tutorial from IBM. It even includes an example of using export.

Tags:

Linux

Terminal