Difference between "a=b" and "export a=b" in bash

54

14

What's the difference between:

a=b

and

export a=b

In bash?

I understand that they both define environment variables, but I don't fully understand the difference.

Adam Matan

Posted 2009-08-06T12:04:25.277

Reputation: 5 930

Could somebody please edit this? This is not at all related to Linux, but depend only on the shell you are using. I guess it's bash here, which also works on Windows. – innaM – 2009-08-06T12:52:17.340

I stand corrected. – Adam Matan – 2009-08-06T12:58:45.260

Answers

58

export propagates the variable to subprocesses.

For example, if you did

FOO=bar

then a subprocess that checked for FOO wouldn't find the variable whereas

export FOO=bar

would allow the subprocess to find it.

But if FOO has already been defined as an environment variable, then FOO=bar will modify the value of that environment variable.

For example:

FOO=one     # Not an environment variable
export FOO  # Now FOO is an environment variable
FOO=two     # Update the environment variable, so sub processes will see $FOO = "two"

Older shells didn't support the export FOO=bar syntax; you had to write FOO=bar; export FOO.

Mike McQuaid

Posted 2009-08-06T12:04:25.277

Reputation: 3 639

34Actually, if you don't use "export", you're not defining an environment variable, but just a shell variable. Shell variables are only available to the shell process; environment variables are available to any subsequent process, not just shells. In addition, subshells are commands contained within parentheses, which do have access to shell variables, whereas what you're talking about are child processes that happen to be shells. – wfaulk – 2009-09-28T12:02:02.720

Where are these stored? – HDave – 2013-03-18T21:45:51.733

29

If you don't use export, you're not defining an environment variable; just a shell variable.

Shell variables are only available to the shell process; environment variables are available to any subsequent process, not just shells.

wfaulk

Posted 2009-08-06T12:04:25.277

Reputation: 5 692

3

Also, if you want to have the variable available to the calling shell without using export you can do this:

File a.ksh is -

#!/bin/ksh
FOO=bar

On the prompt, run this is

> . a.ksh

This will run the commands within the same shell and $FOO will be available.

Whereas,

> a.ksh

Will make $FOO available only within a.ksh, after the call to a.ksh it would not exist.

alok

Posted 2009-08-06T12:04:25.277

Reputation: 390

1Correct. Note that "." is just a shortcut for "source", which is sometimes used in scripts for better readability. See "help ." or "help source" for details. – sleske – 2010-02-11T22:58:27.017

2

In addition to what has already been answered, both of these statement do not necessarily define (i.e. create vs set) an environment variable as "a" might already exist as a shell or environment variable.

In the latter case, both statements are strictly equivalent.

jlliagre

Posted 2009-08-06T12:04:25.277

Reputation: 12 469