Reputation: 41
What is the difference between Exported Environment Variables and Environment Variables?
I have to respond to a question:
How can we display all the environment variables that were defined in terminal?
First, I thought it was printenv
but then again it said DEFINED IN TERMINAL and I thought this means Exported Environment Variables that I've read they're displayed with env
.
I'm kind of confused.
Upvotes: 1
Views: 307
Reputation:
export
is a command that creates an environment variable. The phrase "exported environment variable" is redundant.
The shell may have some environment variables that weren't created with the export
command, because every program starts out with an environment passed by the invoking program through the execve
system call, so I guess you could say there are some environment variables that were never "exported".
But that's a silly distinction. The shell doesn't keep track of the historical origin of its environment variables. There's nothing you can do to make it tell you which ones were "defined in the terminal". It doesn't know. (history | grep export
? Ugh...)
In response to Charles Goodwin's answer, there are no "persistent" environment variables in unix. The illusion of a persistent variable can be created by putting a definition in a shell startup file (/etc/profile
, $HOME/.profile
, etc.) but that definition will be an export
command, indistinguishable from an export
command that you run by hand.
On some systems, an /etc/environment
file exists, which creates an even more powerful illusion of a set of "shared, persistent" environment variables, but actually they are neither. It doesn't contain the export
keyword because it is not parsed by the shell - PAM handles it before starting the shell. It's the same principle as the /etc/profile
though - the file has to be read into your initial process's environment every time you log in. You can see that the values are not shared by trying the "modify and check in another process" experiment on a variable that came from /etc/environment, or even modify the /etc/environment
file and check the effect on already-existing processes - there won't be any.
Environment could more accurately be called "hereditary variables" - information flows only one way through them, from parent to child. It's a bit too late to change the terminology.
Upvotes: 2
Reputation: 6652
Might vary between OSes but my understanding was that exported variables are for that session only (i.e. open a terminal, export an environment variable, open another terminal and the exported env is not set on the new terminal) whereas environment variables are persistent (between sessions, reboots etc).
In terms of how the different types of env apply to applications, I was not aware of any (except obviously if you want the exported env to apply then you have to export it before you run the app).
Upvotes: 0