What are Environment Variables in Linux/Unix

The shell and scripts use variables to store data; some variables can be passed to sub-processes along with their content. These special variables are called environment variables.

Applications and sessions use these variables to determine their behavior. Some of them are likely familiar to administrators, such as PATH, USER, and HOSTNAME, among others. What makes variables environment variables is that they have been exported in the shell. A variable that is flagged as an export will be passed, with its value, to any sub-process spawned from the shell. Users can use the env command to view all environment variables that are defined in their shell.

Any variable defined in the shell can be an environment variable. The key to making a variable become an environment variable is flagging it for export using the export command.

In the following example, a variable, MYVAR, will be set. A sub-shell is spawned, and the MYVAR variable does not exist in the sub-shell.

$ MYVAR="some value"
$ echo $MYVAR
some value
$ bash
$ echo $MYVAR
$ exit

In a similar example, the export command will be used to tag the MYVAR variable as an environment variable, which will be passed to a sub-shell.

$ MYVAR="some value"
$ export MYVAR
$ echo $MYVAR
some value
$ bash
$ echo $MYVAR some value
$ exit