Typing the following in Bash:
env | grep USER
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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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.]
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
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.