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

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

These are the one and same variable. In the shell, as opposed to in most other programming languages, environment variables and shell variables share the same name space. In the shell, an environment variable is a shell variable that have been exported with export.

See, for example, my answer to your previous question "What is the difference in usage between shell variables and environment variables?"


Shell Variable can be used only to current shell, it can't be used in system wide. On the other end Environmental Variable can be used System Wide. By convention Shell Variable are written as lowercase while Environmental Variable are written as uppercase. You can make a shell variable work as an environment variable, you just need to export it.