Shell variable vs. Environment variable, which one is preferred if both have the same name

environment-variablesshell

Typing the following in Bash:

env | grep USER

and

set | grep USER

gives both times the same username.

How do I know, for instance when typing echo $USER if the shell or the environment variable has been displayed?

Best Answer

For POSIX-compatible shells (including Bash), the standard says:

2.5.3 Shell Variables
Variables shall be initialized from the environment [...] If a variable is initialized from the environment, it shall be marked for export immediately; see the export special built-in. New variables can be defined and initialized with variable assignments, [etc.]

And about export:

export name[=word]...
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.

So from the shell's point of view, there are only variables. Some of them may have come from the environment when the shell was started, and some of them may be exported to the environment of the processes the shell starts.

(The "environment" is really just a bunch of strings passed to the process when it starts. When the process is running, it can do whatever it likes with that, use it, ignore it, overwrite it. And what a process passes on when starting other processes can be yet another thing, though of course it's usual to just pass all of the environment variables along again.)


If you were using some non-POSIX shell, such as csh, things might be different:

$ csh
% echo $foo
foo: Undefined variable.
% setenv foo bar
% echo $foo
bar
% set foo=asdf
% echo $foo
asdf
% env |grep foo
foo=bar
% exit
Related Question