Somebody told me that:
BASH isn't an environment variable, it's a shell variable
What's the difference?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
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.
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.
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:
[vishrant@localhost]$ SOME_ENV_VARIABLE="testing.txt" #creating variable in bash shell [vishrant@localhost]$ echo $SOME_ENV_VARIABLE testing.txt [vishrant@localhost]$ export SOME_ENV_VARIABLE #variable now available with environment [vishrant@localhost]$ env | grep SOME_ENV_VARIABLE SOME_ENV_VARIABLE=testing.txt [vishrant@localhost]$ csh #changing shell [vishrant@localhost ~/shell_scripting]$ echo $SOME_ENV_VARIABLE testing.txt [vishrant@localhost ~/shell_scripting]$ exit exit #returned to parent shell [vishrant@localhost]$ SOME_SHELL_VARIABLE="hello.txt" [vishrant@localhost]$ csh [vishrant@localhost ~/shell_scripting]$ echo $SOME_SHELL_VARIABLE SOME_SHELL_VARIABLE: Undefined variable.
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
.bashrc (if you are using bash).