78

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?

15

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.

  • 76
    IMHO copy-pasting an excerpt from documentation without any additional effort of explaining should not be upvoted. – Artur Oct 3 '13 at 16:59
  • 30
    That excerpt is not very clear and frankly I didn't understand what is going on. – Trismegistos Jan 10 '14 at 15:14
  • 4
    @Artur: on the contrary: if the (excerpt from the) documentation answers a question, I'd rather not have additional explanations. – René Nyffenegger Oct 10 '14 at 7:07
  • 2
    @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 Nov 20 '14 at 13:34
  • 2
    I 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 Feb 7 '17 at 2:15
101

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

  • 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 Jun 16 '10 at 20:09
  • 1
    You could check this out: superuser.com/questions/143413/linux-environment-variables/… – BloodPhilia Jun 16 '10 at 20:17
  • 3
    You 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 Jul 28 '14 at 18:49
  • 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 Nov 4 '18 at 12:22
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

Not the answer you're looking for? Browse other questions tagged or ask your own question.