An environment variable is a globally available, in a program and it child programs. A shell variable is only available in the current shell. To make a shell variable available as an environment variable, use export VARNAME (without dollar $).
Examples for clarification:
$ SOME=VAR # define shell variable $SOME
$ echo $SOME
VAR
$ env | grep SOME # note: no output
$ export SOME # turn $SOME into an environment variable
$ env | grep SOME
SOME=VAR
Another way to define an environment variable:
$ export ANOTHER=VALUE
$ echo $ANOTHER
VALUE
$ env | grep ANOTHER
ANOTHER=VALUE
$BASH is a local variable that is valid in the current (bash) shell only.
Environment variables such as $SHELL are valid systemwide. In a current Bash shell, $BASH points to the execution path of bash, whereas $SHELL points to the shell defined as default (which may be of the same value).
For an explanation of environment variables see Environment Variables in Ubuntu Help.
If a change is made to a shell variable, it must be explicitly "exported" to the corresponding environment variable in order for any forked subprocesses to see the change. Recall that shell variables are local to the shell in which they were defined.
To answer this question first try to understand scope of a variable.
When you create a new variable like SOME_ENV_VARIABLE="testing.txt" it resides in the SHELL scope, that means it can be accessed by that instance of shell where the user is logged in. When the instance change for example you open a new terminal or you change the shell (for example you switch to csh) you can not access that variable.
When you export that variable like export SOME_ENV_VARIABLE that variable is now available in environment scope, that means in that instance if you change the shell you can still access that variable. Lets try to understand with following example:
Analogy: let's assume you have a two-bedroom apartment and you are sharing it with another roommate. The common area can be accessed by anyone but not your bedrooms, environment variable is like common area and shell variable is like bedroom, if you will something in common area it can be accessed by anyone but if you keep it in your bedroom it can only be accessed by you.
Remember if open a new terminal you won't be able to access either of the variables because you are changing that instance. For that you should add your variables in either .profile or .bashrc (if you are using bash).
An environment variable is a globally available, in a program and it child programs. A shell variable is only available in the current shell. To make a shell variable available as an environment variable, use
export VARNAME
(without dollar$
).Examples for clarification:
Another way to define an environment variable:
$BASH
is a local variable that is valid in the current (bash) shell only.Environment variables such as
$SHELL
are valid systemwide. In a current Bash shell,$BASH
points to the execution path of bash, whereas$SHELL
points to the shell defined as default (which may be of the same value).For an explanation of environment variables see Environment Variables in Ubuntu Help.
There is a difference. Shell Variables and Environment Variables will explain it better that I can, but here is an excerpt from it:
Shell Variable: Short Term, Applied only to current instance of the shell, Not applicable system wide
Environmental Variable: Long Term Usage, Valid System Wide, Globally Allowed
By convention Shell Variable have their name as lowercase while Envn. Variables are written as uppercase
To answer this question first try to understand scope of a variable.
When you create a new variable like
SOME_ENV_VARIABLE="testing.txt"
it resides in theSHELL scope
, that means it can be accessed by that instance of shell where the user is logged in. When the instance change for example you open a new terminal or you change the shell (for example you switch tocsh
) you can not access that variable.When you export that variable like
export SOME_ENV_VARIABLE
that variable is now available in environment scope, that means in that instance if you change the shell you can still access that variable. Lets try to understand with following example:Analogy: let's assume you have a two-bedroom apartment and you are sharing it with another roommate. The common area can be accessed by anyone but not your bedrooms, environment variable is like common area and shell variable is like bedroom, if you will something in common area it can be accessed by anyone but if you keep it in your bedroom it can only be accessed by you.
Remember if open a new terminal you won't be able to access either of the variables because you are changing that instance. For that you should add your variables in either
.profile
or.bashrc
(if you are using bash).