What is the export command supposed to do in Linux?
Asked
Active
Viewed 2.6k times
3 Answers
9
Export a shell variable as environment variable.

Peter Eisentraut
- 3,665
- 1
- 24
- 21
-
The net result is that when you 'export' a variable, it becomes available as an environment variable within any applications you run within that shell. – McJeff Mar 19 '10 at 20:52
-
Can you show an example usage? – benstpierre Mar 19 '10 at 20:55
-
1Have you tried the `man` page? http://ss64.com/bash/export.html – ceejayoz Mar 19 '10 at 20:58
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
.

Francesco Boi
- 103
- 5

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