What does export do in BASH?

83

22

Possible Duplicate:
Difference between “a=b” and “export a=b” in bash

It is hard to admit, but I have never really understood what exactly export does to an environment variable. I know that if I don't export a variable I sometimes can't see it in child processes, but sometimes it seems like I can. What is really going on when I say

export foo=5

and when should I not export a variable?

Chas. Owens

Posted 2010-06-16T20:02:37.657

Reputation: 1 842

Question was closed 2010-06-17T07:13:12.643

One common use is to add export statements to .bashrc/.bash_profile to create persistent global variables similar to $HOME. – Evan Plaice – 2016-06-10T05:16:31.523

Careful, there is more to this story than initially appears. I invite you to check my answer.

– jasonleonhard – 2017-02-07T03:48:01.153

Here's a link to at least one other helpful question on this topic: http://superuser.com/questions/143413/linux-environment-variables/ ... since ironically this question was the first one that popped up on Google for my query on export in bash.

– Ogre Psalm33 – 2011-04-13T18:07:15.700

Answers

16

From man bash:

ENVIRONMENT

When a program is invoked it is given an array of strings called the environment. This is a list of name-value pairs, of the form name=value.

The shell provides several ways to manipulate the environment. On invocation, the shell scans its own environment and creates a parameter for each name found, automatically marking it for export to child processes. Executed commands inherit the environment. The export and declare -x commands allow parameters and functions to be added to and deleted from the environment. If the value of a parameter in the environment is modified, the new value becomes part of the environment, replacing the old. The environment inherited by any executed command consists of the shell's initial environment, whose values may be modified in the shell, less any pairs removed by the unset command, plus any additions via the export and declare -x commands.

sml

Posted 2010-06-16T20:02:37.657

Reputation: 1 582

4@Artur: on the contrary: if the (excerpt from the) documentation answers a question, I'd rather not have additional explanations. – René Nyffenegger – 2014-10-10T07:07:59.287

3@RenéNyffenegger but it seems, it doesn't. At least, I didn't get it until I read an answer by BloodPhilia which should be marked as accepted. – Vladislav Rastrusny – 2014-11-20T13:34:31.763

2I believe the points Artur and Trismegistos were making is anyone can copy paste, its not always enough, clarify and provide a good answer. It's fine to copy paste some documentation or provide a link, in fact it is encouraged, but there should be some additional quality explanation. Furthermore, that explanation can easily be ignored by people like RenéNyffenegger if they do not need it, but it will be there for those who will benefit from it. We are all trying to learn and have different ways we gain understanding, this helps cover a variety of learning styles and will improve your rating. – jasonleonhard – 2017-02-07T02:15:08.527

My comment above took more time and care than the answer provided by sml and currently that answer is marked accepted by the OP. That is another point to consider. – jasonleonhard – 2017-02-07T02:21:28.033

80IMHO copy-pasting an excerpt from documentation without any additional effort of explaining should not be upvoted. – Artur – 2013-10-03T16:59:52.060

34That excerpt is not very clear and frankly I didn't understand what is going on. – Trismegistos – 2014-01-10T15:14:57.573

107

Exported variables get passed on to child processes, not-exported variables do not.

BloodPhilia

Posted 2010-06-16T20:02:37.657

Reputation: 27 374

3You can verify this by adding something to a path (say to PYTHONPATH) and then noting that though you can echo $PYTHONPATH it does not get recognized by python or bash scripts until you export it – Kaushik Ghose – 2014-07-28T18:49:42.510

Can you point to any documentation to that affect. I am looking for more information than that. For instance, does a variable only need to be exported once, or do you need to export it after every change, etc. – Chas. Owens – 2010-06-16T20:09:57.677

1

You could check this out: http://superuser.com/questions/143413/linux-environment-variables/143418#143418

– BloodPhilia – 2010-06-16T20:17:40.877

This answer does not seem to be entirely true either. Bash sub-shells, for example, are to child-processes (according to $BASHPID) and yet you can read unexported variables from the parent shell. Simple proof: x="y"; echo "$BASHPID: $x"; (echo "$BASHPID: $x") My guess is that this is a special case which occurs when the child process is a sub-shell. – JepZ – 2018-11-04T12:22:22.467

20

When you use export, you are adding the variable to the environment variables list of the shell in which the export command was called and all the environment variables of a shell are passed to the child processes, thats why you can use it.

When you finish the shell its environment is destroyed, thats why the environment variables are declared and exported at login, in the .bashrc file for example

alfredozn

Posted 2010-06-16T20:02:37.657

Reputation: 337