56

Somebody told me that:

BASH isn't an environment variable, it's a shell variable

What's the difference?

  • sounds like the same thing just has a different scope? – Alexander Mills May 31 at 20:40
34

$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.

  • 1
    A great link (+1) :) – Peter.O Feb 14 '11 at 14:42
  • 12
    This answer is incorrect/inaccurate. Environment variables are NOT global/system-wide. Shell variables are private to the currently running shell, and they are NOT exported (passed on) to any child processes. Environment variables are exported to any child processes. "MYVAR=private ; export OURVAR=shared ; printenv" would only report "OURVAR" because when the shell executes the printenv command, it exports OURVAR, but not MYVAR. – C. M. Dec 31 '17 at 4:10
  • 2
    +1 to C.M.'s Comment! I've never heard the difference explained so simply and accurately!!! This comment should be the answer. – Jesse the Wind Wanderer Apr 14 at 4:40
48

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
  • I don't think this is correct. export does not necessarily set an environment variable. stackoverflow.com/questions/7411455/… – Chris Stryczynski Nov 17 '17 at 17:41
  • 3
    @ChrisStryczynski export does set an environment variable for child processes. The post you linked does not contradict this answer, could you clarify why you think it is wrong? – Lekensteyn Nov 19 '17 at 23:12
  • export absolutely set an environment variable – David Tonhofer Dec 24 '18 at 15:22
8

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.

  • I'm commenting my own answer.. I really like the Help Ubuntu link in Takkat's answer... (it's better than mine) – Peter.O Feb 14 '11 at 14:46
  • 3
    Plus your link no longer works! – user12753 Sep 19 '17 at 15:19
1

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

-1

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 .profile or .bashrc (if you are using bash).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.