Difference between shell and environment variables

19,381

Solution 1

Citing this source,

Standard UNIX variables are split into two categories, environment variables and shell variables. In broad terms, shell variables apply only to the current instance of the shell and are used to set short-term working conditions; environment variables have a farther reaching significance, and those set at login are valid for the duration of the session. By convention, environment variables have UPPER CASE and shell variables have lower case names.

To list all environment variables, use printenv and to list all shell variables, use set.

You'll note that the environment variables store more permanent value, e.g.:

HOME=/home/adam

Which changes quite seldom, while the shell variables stores local, temporary, shell-specific values, e.g.:

PWD=/tmp

which changes every time you change your current directory.

For most practical tasks, set environment values by adding export VARIABLE_NAME=VALUE to your ~/.bashrc file.

Solution 2

Their difference is similar to the difference between private fields and protected fields in a Java class.

The private fields of a Java class is only accessible from that Java class. The protected fields of a Java class is accessible from both that Java class and its subclasses.

The shell variables of a shell is only accessible from that shell process. The environment variables exported from that shell is accessible from both that shell process and the sub-processes created from that shell.

Solution 3

For Bash shell:

Shell variables differ from environment variables in different ways:

♦ A shell variable is specific to the shell itself and is not inherited by child processes. For example, let's say you're running another application from the shell, that application will not inherit the shell variable:

$ SHELL_VAR=xyz
$ firefox

SHELL_VAR will not be available in the environment of the child process (firefox).

♦ In contrast, environment variables of the parent process (the shell here) are inherited by all child processes:

$ export SHELL_VAR=xyz
$ firefox

♦ Both shell and environment variables are local to the shell/process which defined them:

Environment variables can be persistent, whereas, for shell variables once you exit the session, they're all gone.

Note: the above examples only alter the shell that you're working on, in other words, if you logout or start a new shell/terminal you're not going to see the variables that you defined, this is per the principle of process locality.


How to make presistent shell variables:

One way to do that is by modifying the ~/.profile file:

export SHELL_VAR=xyz

This setting is user-specific and not system-wide, for system-wide environment variables, you can add the above line to a .sh file in /etc/profile.d

I highly recommend reading this page: EnvironmentVariables

Share:
19,381
sunil
Author by

sunil

Updated on June 03, 2022

Comments

  • sunil
    sunil almost 2 years

    What are the differences between shell and environment variables? Where are these variables stored?