Difference between shell and environment variables
Asked Answered
R

5

41

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

Ranunculaceous answered 27/7, 2010 at 7:7 Comment(0)
I
34

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.

Infinitude answered 27/7, 2010 at 7:11 Comment(7)
environment variable are user defined ? & shell variable s are system define ? also u had mentioned the commands not the location i means where the variable are located ???Ranunculaceous
What do you mean by 'Where the variables are located'? Where do you set their value, or where are the (technically) stored in the computer's memory?Infinitude
any file is there where they are stored && are they userdefined or predefinedRanunculaceous
when we use set or env command from where the output is called from(i mean whatz the designation file or memory )Ranunculaceous
You quoted "By convention, environment variables have UPPER CASE and shell variables have lower case names.", but you were showing us that upper case are used for for both shell and environment variables.Cyme
To provide a more canonically correct source, see the Environment Variables section of the POSIX specification at pubs.opengroup.org/onlinepubs/9699919799/basedefs/…. Uppercase is used for variables -- either exported or otherwise -- that can modify the behavior of the shell and of POSIX-specified tools; whereas lowercase should be used for variables, including environment variables, that are application-defined and not expected to modify POSIX-specified tool behavior.Constabulary
@zell, see above. By POSIX spec, uppercase is used for variables that reflect or modify shell and POSIX-specified tool behavior, whereas names with at least one lowercase character are reserved for application-defined use.Constabulary
M
19

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

Mumford answered 3/6, 2017 at 20:3 Comment(1)
Fair to say then that 'persistent' environment variables are in no way actually different to an exported shell variable. The only difference is in where they are stored. Persistent ones are written to (and then read) from non-volatile media (i.e. disk). This is in a location (i.e. ~/.profile) that is automatically read when the shell is loaded. An exported shell variable is identical, except it exists only in volatile memory and is therefore lost on a reboot (power loss, etc). Is that correct?Refractory
K
15

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.

Kathlyn answered 24/12, 2012 at 20:2 Comment(0)
S
0

In addition to the information in the other answers, environment variables are not as flexible as shell variables.

Shell variables can be arrays or associative arrays, but environment variables can only be strings. The environment that's inherited by child processes is simply a sequence of name=value strings. There's no parsing of the values, so no way to indicate that a value should be an array.

Sacculus answered 24/8, 2023 at 22:58 Comment(0)
R
-2

A shell variable is just a special case of an environment variable. shell variables are inherited from the environment and possibly copied to the environment of children of the shell depending on syntax used: http://www.pixelbeat.org/docs/env.html

Rocher answered 27/7, 2010 at 11:52 Comment(3)
This is not correct. You basically have the terms reversed. Obviously there is a lot of confusion on this topic, even by veteran users and a coreutils dev? :-/Symons
I don't think I've reversed the terms. I was implying that the environment list is more fundamental and present for all processes including shells. shell variables are a special case that are initialised from the environment listRocher
But you can create shell variables that aren't environment variables. When you do foo=bar it creates a shell variable. It becomes an environment variable when you do export foo.Sacculus

© 2022 - 2024 — McMap. All rights reserved.