2

I want to clarify a confusion I am having in shell variable vs environment variable. I did the following test, where I have a shell variable abc and export it to an environment variable.

$ 
$ abc="shell var"
$ env | grep abc
$ echo $abc
shell var
$ export abc="env var"
$ env | grep abc
abc=env var
$ echo $abc
env var
$ unset abc
$ env | grep abc
$ echo $abc
$ 

After the export is done, I try to echo $abc.

Questions:

  • Does export move the variable abc from shell to the environment OR does it create a copy in the environment and assign it a new value ?

  • When the second echo is done after the export, does echo check if abc is in environment and then print it, OR has abc been completely removed from the shell and is only present in the environment which is why echo prints its value ?

marked as duplicate by cuonglm, Archemar, Stephen Kitt, Anthon, yaegashi Aug 13 '15 at 9:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

Does export move the variable abc from shell to the environment OR does it create a copy in the environment and assign it a new value ?

Neither. export simply marks a variable for export.

When an external command is executed, the shell creates an environment to pass to it. Only variables marked for export are added to that environment. As man bash explains:

shell variables and functions marked for export, along with variables exported for the command, [are] passed in the environment

Note that env is an external command. Consequently, it can only report on variables that have been exported.

Meaning of "variables exported for the command"

Normal variable assignments have persistence: the variable exists until it is removed (unset). It is also possible to created temporary variables for use by a particular command.

As an example, let's create a variable a for use by the env command:

$ a=b env | grep ^a=
a=b
  • What does "variables exported for the command" mean ? How are they different from the variables marked for export ? Thanks. – Jake Aug 13 '15 at 5:01
  • Also: when I do export abc="env var", does that mean : change the value for this shell variable, but also mark this for export ? I guess that's why echo $abc prints env var still being in the shell process ? – Jake Aug 13 '15 at 5:05
  • 1
    @Jake Yes, export abc="env var" does the assignment and marks the variable for export all in one step. Also, I added to the answer an example of a "variable exported for the command." – John1024 Aug 13 '15 at 5:08
  • I had another question. When a command is run in bash, some environment variables will be given to the new process by default - where are all these environment variable (like SHELL) marked for export ? (Let me know if I should a new question for this) – Jake Aug 13 '15 at 7:04
  • @Jake Unlike user variables, the variable SHELL is managed directly by the shell. The shell manages many such variables. For a full list of shell such variables set by bash, look in man bash for the section entitled "Shell Variables". – John1024 Aug 13 '15 at 7:09

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