LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   why do we have to export variables? (https://www.linuxquestions.org/questions/linux-newbie-8/why-do-we-have-to-export-variables-327630/)

learnfast 05-27-2005 06:53 AM

why do we have to export variables?
 
I'm not clear on why we have to export variables in linux.

For example, in a programming language you define a variable and use it. In Windows, you define an environment variable and use it. In Linux you define a variable and then you have to export it. Where are we exporting it to?

Ok, I assume someone will say "outside the shell", but what does that mean? Is there a global variable area outside the shell where variables can be exchanged between shells? Is this the only place to which variables can be "exported"? Is this similar to private and public variables in programming?

Is there any way I can define WHICH other shells have access to my exported variables? Do all other applications running have access to my exported variables? What if another shell exports a variable with the same name but a different value, does it overwrite mine?

This is all a bit foggy.

Thanks.

mlangdn 05-27-2005 07:24 AM

export VARIABLE

This adds the variable to the process environment. Under Bash (but not under the old Bourne shell) it is also possible
to declare a variable to be global on a single line by

export GLOBALVAR="global"

See also Man Page: export-1

I am not a programmer, so I have very little experience with variables. I hope this gets you the info you were seeking.

kees-jan 05-27-2005 08:27 AM

All processes have a process environment, containing environment variables.

In addition to that, shells have a local environment, containing local variables.

When you invoke an application, it inherits the process environment (containing the environment variables) from the shell, but not the local environment.

So if you are using a variable, and you want applications or commands that you execute to be able to see the variable as well, then you have to export it. Otherwise you don't.

It is important to note that if you start an application or command, it gets its own copy of the enivronment. Hence, you applications can never change any variables in your shell.

Groetjes,

Kees-Jan


All times are GMT -5. The time now is 05:50 PM.