Export

From LQWiki
Jump to navigation Jump to search

export is a command to set environment variables in the bash shell.

Examples:

$ PATH="$PATH:."
$ export $PATH

or

$ export PATH=$PATH:.

verify a variable has been set:

$ export sense=42
$ echo $sense
42

You can list all environment variables currently set with the command env.

Usecase

At first glance, export is useless, because you can as well omit it:

$ export a=b
$ echo $a
b
$ x=y
$ echo $x
y

So, what is the difference between

$ export first="hello"

and

$ first="hello"

The difference is that "export" sets an environment variable that you can show with the command

env

And that will be available to sub-contexts, so, to programs that will be called from this shell. As an example, let's write a file output.sh

echo $first
echo $second

now we set $first different from $second:

$ chmod 777 output.sh
$ first=hello
$ export second=world
$ ./output.sh

world

You see, the (assigned) value of $first is not available to output.sh, but the (exported) value of $second is.