10

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?

GAD3R
  • 66,769
sharkant
  • 3,710

3 Answers3

14

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
ilkkachu
  • 138,973
  • 1
    Note that the Bourne shell, like csh initialises shell variables from environment variables. But modifying shell variables doesn't affect the corresponding environment variable unless you export them. That's something that was broken by the Korn shell (and specified by POSIX). That's why you should be careful with the name of the shell variables you use now to make sure that you're not modifying env vars that could affect commands that you run in your script. – Stéphane Chazelas May 18 '17 at 13:23
4

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?"

Kusalananda
  • 333,661
  • i will reread your explanation, only I need to learn some things first, before I can understand your answer. – sharkant May 12 '17 at 14:19
  • @sharkant No worries at all. If I'm confusing things, then just tell me and I'll try to clarify. ilkkachu's answer is good too. – Kusalananda May 12 '17 at 14:22
  • no , i do not think so , you have a good style of explanation, it is only my lack of knowledge which can not cherish them yet. – sharkant May 12 '17 at 14:32
2

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.

N.Ahmed
  • 31
  • 5