9

What is the export command supposed to do in Linux?

benstpierre
  • 265
  • 1
  • 3
  • 7

3 Answers3

9

Export a shell variable as environment variable.

Peter Eisentraut
  • 3,665
  • 1
  • 24
  • 21
8

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.

Zoredache
  • 130,897
  • 41
  • 276
  • 420
2

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

mctylr
  • 863
  • 4
  • 9
  • I know it's generally against SE policy to recommend an external link (they have a habit of dying), however the page is far too large and detailed to pull into an answer. I gave a +1 because Bash by Example is an *excellent* resource. And far better than `man`. – Django Reinhardt Feb 16 '21 at 17:36