-->

Looking at Global And Local Environment Variables

Global environment variables are visible from the shell session and from any spawned child subshells. Local variables are available only in the shell that creates them. This makes global environment variables useful in applications that create child subshells, which require parent shell information.

Looking at global environment variables
Looking at global environment variables


The Linux system sets several global environment variables when you start your bash session. (For more details about what variables are started at that time, see the “Locating System Environment Variables” section later in this chapter.) The system environment variables almost always use all capital letters to differentiate them from normal user environment variables.

To view global environment variables, use the env or the printenv command:

$ printenv
HOSTNAME=server01.class.edu
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
[…]
HOME=/home/Christine
LOGNAME=Christine
[…]
G_BROKEN_FILENAMES=1
_=/usr/bin/printenv

So many global environment variables get set for the bash shell that the display had to be snipped. Not only are many set during the login process, but how you log in can affect which ones are set as well.

To display an individual environment variable’s value, you can use the printenv command, but not the env command:

$ printenv HOME
/home/Christine
$$
env HOME
env: HOME: No such file or directory
$

You can also use the echo command to display a variable’s value. When referencing an environment variable in this case, you must place a dollar sign ($) before the environment variable name:

$ echo $HOME
/home/Christine
$

Using the dollar sign along with the variable name does more than just display its current definition when used with the echo command. The dollar sign before a variable name allows the variable to be passed as a command parameter:

$ ls $HOME
Desktop Downloads Music Public test.sh
Documents junk.dat Pictures Templates Videos
$$
ls /home/Christine
Desktop Downloads Music Public test.sh
Documents junk.dat Pictures Templates Videos
$
As mentioned earlier, global environment variables are also available to any process’s
subshells:
$ bash
$$
ps -f
UID PID PPID C STIME TTY TIME CMD
501 2017 2016 0 16:00 pts/0 00:00:00 -bash
501 2082 2017 0 16:08 pts/0 00:00:00 bash
501 2095 2082 0 16:08 pts/0 00:00:00 ps -f
$$
echo $HOME
/home/Christine
$$
exit
exit
$

In this example, after spawning a subshell using the bash command, the HOME environment variable’s current value is shown. It is set to the exact same value, /home/Christine, as it was in the parent shell.

Local environment variables

Local environment variables, as their name implies, can be seen only in the local process in which they are defined. Even though they are local, they are just as important as global environment variables. In fact, the Linux system also defines standard local environment variables for you by default. However, you can also define your own local variables.

These, as you would assume, are called user-defined local variables.

Trying to see the local variables list is a little tricky at the CLI. Unfortunately, there isn’t a command that displays only these variables. The set command displays all variables defined for a specific process, including both local and global environment variables and user-defined variables:

$ set
BASH=/bin/bash
[…]
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
[…]
colors=/etc/DIR_COLORS
my_variable=‘Hello World’
[…]
$

All global environment variables displayed using the env or printenv commands appear in the set command’s output. The additional environment variables are the local environment and user-defined variables.

Note
The differences between the commands env, printenv, and set are subtle. The set command displays both global and local environment variables and user-defined variables. It also sorts the display alphabetically. The env and printenv are different from set in that they do not sort the variables, nor do they include local environment or local user-defined variables. Used in this context, env and printenv produce duplicate listings. However, the env command has additional functionality that printenv does not have, making it the slightly more powerful command.

0 Response to "Looking at Global And Local Environment Variables"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel