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?


3 Answers 3


From man bash:


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.

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

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

  • 1
    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. Jun 16, 2010 at 20:09
  • 1
    You could check this out: superuser.com/questions/143413/linux-environment-variables/…
    – Pylsa
    Jun 16, 2010 at 20:17
  • 5
    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 Jul 28, 2014 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, 2018 at 12:22
  • 3
    @JepZ I think the examples you've given don't apply. these are examples of either shell builtins echo which is not a sub-process, or shell expansions which occur in the same shell.
    – Samha'
    May 21, 2020 at 15:27

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 .