Pages

Shell Variables vs. Environment Variables in Linux

What is the difference between Linux Shell Variables and Environment Variables?

 This is a common question often asked in Linux administration job interviews.  The key to a clear answer is to describe the scope of the two types of variables. Basically, shell variables are only available to the current invocation of the shell; environment variables become properties of the system and transcend shell invocation.  That means, a shell variable defined in one shell will not be seen if you switch to another shell, but you can see the environment variables defined in other shells.

In the following example, we defined a shell variable DBS in Bourne shell, after we switch to bash shell, this variable is not avaliable:

$ DBS=5
$ bash
bash $ echo $DBS
... no output ...

Then we try to define an environment variable:

$ DBS=5
$ export DBS
$ bash
base $ echo $DBS
5

Note that we need to use "export" command to define an environment variable.  Also, the "$" sign should be added in front of the variable when referring the variable, but the preceding $ is not needed when defining the variable.

No comments:

Post a Comment