Difference between environment variables and exported environment variables in bash
Clash Royale CLAN TAG#URR8PPP
up vote
36
down vote
favorite
Bash seems to differentiate between variables which have been exported and those which have not.
example:
$ FOO=BAR
$ env | grep FOO
$ set | grep FOO
FOO=BAR
set
sees the variable but env
does not.
$ export BAR=FOO
$ env | grep FOO
BAR=FOO
$ set | grep FOO
BAR=FOO
FOO=BAR
set
sees both variables but env
sees only the exported variable.
I know that set
is a bash builtin and env
is not.
What are the differences between variables which are exported and those which are not?
shell environment-variables
add a comment |Â
up vote
36
down vote
favorite
Bash seems to differentiate between variables which have been exported and those which have not.
example:
$ FOO=BAR
$ env | grep FOO
$ set | grep FOO
FOO=BAR
set
sees the variable but env
does not.
$ export BAR=FOO
$ env | grep FOO
BAR=FOO
$ set | grep FOO
BAR=FOO
FOO=BAR
set
sees both variables but env
sees only the exported variable.
I know that set
is a bash builtin and env
is not.
What are the differences between variables which are exported and those which are not?
shell environment-variables
13
Terminology note: an âÂÂenvironment variableâ is always exported. A non-exported variable is a âÂÂshell variableâ (or âÂÂparameterâÂÂ).
â Gilles
Sep 13 '12 at 0:28
add a comment |Â
up vote
36
down vote
favorite
up vote
36
down vote
favorite
Bash seems to differentiate between variables which have been exported and those which have not.
example:
$ FOO=BAR
$ env | grep FOO
$ set | grep FOO
FOO=BAR
set
sees the variable but env
does not.
$ export BAR=FOO
$ env | grep FOO
BAR=FOO
$ set | grep FOO
BAR=FOO
FOO=BAR
set
sees both variables but env
sees only the exported variable.
I know that set
is a bash builtin and env
is not.
What are the differences between variables which are exported and those which are not?
shell environment-variables
Bash seems to differentiate between variables which have been exported and those which have not.
example:
$ FOO=BAR
$ env | grep FOO
$ set | grep FOO
FOO=BAR
set
sees the variable but env
does not.
$ export BAR=FOO
$ env | grep FOO
BAR=FOO
$ set | grep FOO
BAR=FOO
FOO=BAR
set
sees both variables but env
sees only the exported variable.
I know that set
is a bash builtin and env
is not.
What are the differences between variables which are exported and those which are not?
shell environment-variables
edited Dec 23 '12 at 14:59
asked Oct 25 '10 at 22:15
lesmana
13.6k105469
13.6k105469
13
Terminology note: an âÂÂenvironment variableâ is always exported. A non-exported variable is a âÂÂshell variableâ (or âÂÂparameterâÂÂ).
â Gilles
Sep 13 '12 at 0:28
add a comment |Â
13
Terminology note: an âÂÂenvironment variableâ is always exported. A non-exported variable is a âÂÂshell variableâ (or âÂÂparameterâÂÂ).
â Gilles
Sep 13 '12 at 0:28
13
13
Terminology note: an âÂÂenvironment variableâ is always exported. A non-exported variable is a âÂÂshell variableâ (or âÂÂparameterâÂÂ).
â Gilles
Sep 13 '12 at 0:28
Terminology note: an âÂÂenvironment variableâ is always exported. A non-exported variable is a âÂÂshell variableâ (or âÂÂparameterâÂÂ).
â Gilles
Sep 13 '12 at 0:28
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
38
down vote
accepted
Exported variables are carried into the environment of processes started by the shell that exported them, while non-exported variables are local to the current process only. From the export
man page:
The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands.
set
outputs the current environment, which includes any local non-exported variables. env
is used to launch programs in a new environment, and with no arguments will output what that new environment would be. Since env
is creating a new environment, only exported variables are brought through, as is the case for any program launched from that shell. For example, spawning a second shell within the first (I used $$
to represent prompts in the inner shell):
$ FOO=BAR
$ bash
$$ echo $FOO # Note the empty line
$$ exit
$ export FOO
$ bash
$$ echo $FOO
BAR
$$
Edited to show that it's the variable itself that's exported, not the value. Once you export FOO
, FOO
becomes a global variable and shows up in subsequent environments, even if changed later:
$ export FOO
$ FOO=BAR
$ bash
$$ echo $FOO
BAR
$$
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
38
down vote
accepted
Exported variables are carried into the environment of processes started by the shell that exported them, while non-exported variables are local to the current process only. From the export
man page:
The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands.
set
outputs the current environment, which includes any local non-exported variables. env
is used to launch programs in a new environment, and with no arguments will output what that new environment would be. Since env
is creating a new environment, only exported variables are brought through, as is the case for any program launched from that shell. For example, spawning a second shell within the first (I used $$
to represent prompts in the inner shell):
$ FOO=BAR
$ bash
$$ echo $FOO # Note the empty line
$$ exit
$ export FOO
$ bash
$$ echo $FOO
BAR
$$
Edited to show that it's the variable itself that's exported, not the value. Once you export FOO
, FOO
becomes a global variable and shows up in subsequent environments, even if changed later:
$ export FOO
$ FOO=BAR
$ bash
$$ echo $FOO
BAR
$$
add a comment |Â
up vote
38
down vote
accepted
Exported variables are carried into the environment of processes started by the shell that exported them, while non-exported variables are local to the current process only. From the export
man page:
The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands.
set
outputs the current environment, which includes any local non-exported variables. env
is used to launch programs in a new environment, and with no arguments will output what that new environment would be. Since env
is creating a new environment, only exported variables are brought through, as is the case for any program launched from that shell. For example, spawning a second shell within the first (I used $$
to represent prompts in the inner shell):
$ FOO=BAR
$ bash
$$ echo $FOO # Note the empty line
$$ exit
$ export FOO
$ bash
$$ echo $FOO
BAR
$$
Edited to show that it's the variable itself that's exported, not the value. Once you export FOO
, FOO
becomes a global variable and shows up in subsequent environments, even if changed later:
$ export FOO
$ FOO=BAR
$ bash
$$ echo $FOO
BAR
$$
add a comment |Â
up vote
38
down vote
accepted
up vote
38
down vote
accepted
Exported variables are carried into the environment of processes started by the shell that exported them, while non-exported variables are local to the current process only. From the export
man page:
The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands.
set
outputs the current environment, which includes any local non-exported variables. env
is used to launch programs in a new environment, and with no arguments will output what that new environment would be. Since env
is creating a new environment, only exported variables are brought through, as is the case for any program launched from that shell. For example, spawning a second shell within the first (I used $$
to represent prompts in the inner shell):
$ FOO=BAR
$ bash
$$ echo $FOO # Note the empty line
$$ exit
$ export FOO
$ bash
$$ echo $FOO
BAR
$$
Edited to show that it's the variable itself that's exported, not the value. Once you export FOO
, FOO
becomes a global variable and shows up in subsequent environments, even if changed later:
$ export FOO
$ FOO=BAR
$ bash
$$ echo $FOO
BAR
$$
Exported variables are carried into the environment of processes started by the shell that exported them, while non-exported variables are local to the current process only. From the export
man page:
The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands.
set
outputs the current environment, which includes any local non-exported variables. env
is used to launch programs in a new environment, and with no arguments will output what that new environment would be. Since env
is creating a new environment, only exported variables are brought through, as is the case for any program launched from that shell. For example, spawning a second shell within the first (I used $$
to represent prompts in the inner shell):
$ FOO=BAR
$ bash
$$ echo $FOO # Note the empty line
$$ exit
$ export FOO
$ bash
$$ echo $FOO
BAR
$$
Edited to show that it's the variable itself that's exported, not the value. Once you export FOO
, FOO
becomes a global variable and shows up in subsequent environments, even if changed later:
$ export FOO
$ FOO=BAR
$ bash
$$ echo $FOO
BAR
$$
edited May 5 '15 at 5:16
Azhrei
33818
33818
answered Oct 25 '10 at 22:25
Michael Mrozekâ¦
58.1k26184206
58.1k26184206
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f3507%2fdifference-between-environment-variables-and-exported-environment-variables-in-b%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
13
Terminology note: an âÂÂenvironment variableâ is always exported. A non-exported variable is a âÂÂshell variableâ (or âÂÂparameterâÂÂ).
â Gilles
Sep 13 '12 at 0:28