Environment variable vs Shell variable, what's the difference?
Somebody told me that:
BASH isn't an environment variable, it's a shell variable
What's the difference?
Solution 1:
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
Solution 2:
$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.
Solution 3:
There is a difference. Shell Variables and Environment Variables will explain it better that I can, but here is an excerpt from it:
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.