J
Jarret W. Buse
Guest
Linux+: Linux Shell 02 – Environment Variables
In the Linux Shell Introduction, you saw a few environment variables. The variables were $SHELL and $HISTSIZE. Now we will cover the rest of these environment variables.
First, let's look at what the environment is in the Operating System (OS). Each program which runs is called a process. Each process is run within an environment. The environment consists of the input and output files. Input files are those needed by the program such as library files and the like. Examples of output files are saved data and temporary files. Each process also contains the Environment Variables which are configurable options to control the environment.
The environment is easily controlled within a shell. Each command you enter into the shell may create a new process. The programs you execute from a shell create a new process, but some commands may only change the shell such as 'ls'. The example command 'ls' displays a list of the directories and files within the current directory location.
There are five possible states for each process:
SSH_AGENT_PID=1985
DEVTYPE=drm_minor
SHELL=/bin/bash
TERM=xterm
KERNEL=card0
XDG_SESSION_COOKIE=d18aaa552dc2704636b4997c0000000a-1402844511.879491-1867820031
SUBSYSTEM=drm
E_DATA_DIR=/usr/share/enlightenment
GNOME_KEYRING_CONTROL=/home/jarret/.cache/keyring-GzDy5Z
E_START_TIME=1402844512.8
PRIMARY_DEVICE_FOR_DISPLAY=1
DEVPATH=/devices/pci0000:00/0000:00:01.0/0000:01:05.0/drm/card0
USER=jarret
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00
i=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01
r=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42
w=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:
XTERM_256_COLORS=1
E_BIN_DIR=/usr/bin
TAGS=:udev-acl:
E_SCALE=1.200
SSH_AUTH_SOCK=/tmp/ssh-pGlwusaj1947/agent.1947
DEFAULTS_PATH=/usr/share/gconf/Enlightenment.default.path
E_ICON_THEME=MaXo-ReMix
XDG_CONFIG_DIRS=/etc/xdg/xdg-Enlightenment:/etc/xdg
MINOR=0
E_LIB_DIR=/usr/lib
PATH=/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
DESKTOP_SESSION=Enlightenment
ACTION=add
RUNLEVEL=2
JOB=udev-fallback-graphics
PWD=/home/jarret
UDEV_LOG=3
RESULT=ok
LANG=en_US.UTF-8
GNOME_KEYRING_PID=1871
TERMINOLOGY=1
MANDATORY_PATH=/usr/share/gconf/Enlightenment.mandatory.path
PREVLEVEL=N
MAJOR=226
USEC_INITIALIZED=14856703
E_RESTART=1
PANTS=ON
E_LOCALE_DIR=/usr/share/locale
E_CONF_PROFILE=Fancy
DEVNAME=/dev/dri/card0
HOME=/home/jarret
SHLVL=1
XORGCONFIG=/etc/X11/xorg.conf
UPSTART_INSTANCE=
UPSTART_EVENTS=filesystem runlevel started drm-device-added stopped
LOGNAME=jarret
E_PREFIX=/usr
E_START=/usr/bin/enlightenment_start
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-AGuUZ6VYq4,guid=5d0fb7dfbc6bc601bb12c8fd00000026
XDG_DATA_DIRS=/usr/share/enlightenment:/usr/share:/usr/share/Enlightenment:/usr/local/share/:/usr/share/
LESSOPEN=| /usr/bin/lesspipe %s
DESKTOP=Enlightenment-0.17.0
UPSTART_JOB=lxdm
INSTANCE=
DISPLAY=:0.0
LESSCLOSE=/usr/bin/lesspipe %s %s
E_IPC_SOCKET=/tmp/e-jarret@0/:0.0-1990
XAUTHORITY=/home/jarret/.Xauthority
SEQNUM=1538
_=/usr/bin/env
Some of these Environment Variables are used by the OS and each distro can vary. There are Environment Variables that are important to know. These variables are as follows:
To display any of these variables for your system, go to a Terminal and type 'echo $VARIABLE'. For example, to find my Home directory I type 'echo $HOME' and the result is '/home/jarret'.
NOTE: The command 'echo' is used to display what is followed by the command. The dollar sign is used to signify that the contents of the variable are to be displayed. Without the dollar sign, the typed word would be printed back.
To set an Environment Variable, you can do one of the following exports:
For example, let's look at my existing PATH variable. It contains the values '/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games'. If a new directory was made which contained program files I wrote and wished to use in a shell, I would need to add it to the PATH. Let's assume the directory is /home/jarret/commands/. To add this directory to the PATH variable would require one of the following commands:
NOTE: Note the strings are enclosed in quotes.
By adding the $PATH as part of the new PATH name, it includes the existing path so there is no need to retype all of the paths. If you want to type it all in and remove entries, then you would not use the $PATH. For example, to remove 'usr/games' from the original PATH, I would perform one of the following:
NOTE: Each directory is separated by a colon
).
Two very important variables to know are the PS1 and UNAME variables since they can require parameters.
The PS1 variable controls the Shell prompt. The default prompt for my system is; \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$. This seems like a lot of gibberish, but it will make sense soon.
The parameters for the PS1 variable are as follows:
If we look back at my PS1 variable contents, we see the following:
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$. Let's remove the ANSI color codes first and get: '${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '. Here, the {debian_chroot:+($debian_chroot)}is used when a chroot is set and you change roots. Most people do not use the chroot, so for now we can ignore this. After the chroot information we see \u@\h:\w\$. This shows the shell prompt will be the username@hostname:directory$. All parameters must begin with a \ or it is taken as a literal character which will be printed on the screen.
The ANSI color codes are as follows:
To use the ANSI codes you start with a \033[01;##m\]. Looking back at my sample PS1, you can see that the user and hostname are green. The directory is blue. Using the [00m\] causes the color to return to default (same as [01;39m\]). To change the username and hostname to magenta and the directory to cyan, the following code would be used:
export PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;35m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w[\033[00m\]\$"
NOTE: The long string used to create the prompt is enclosed in quotes.
To add a background color, add \[033[01;47m\] after the foreground color and at the end where it should stop, add \[033[01;49m\].
The shell command 'uname' is used more for getting system information. This information cannot be changed only viewed. By typing uname in the shell, I get back the following:
Linux
This shows I am using a Linux OS. The parameters for uname are:
Linux jarret-Presario-CQ62-Notebook-PC 3.8.0-19-generic #30~precise1-Ubuntu SMP
Wed May 1 22:26:36 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
The jarret-Presario-CQ62-Notebook-PC is for the -n parameter. The 3.8.0-19-generic is for the -r parameter which is the release version. The -v displays the kernel build which in this case is the #30~precise1-Ubuntu SMP Wed May 1 22:26:36 UTC 2013 message. The -m produces the architecture: x86_64. The OS is GNU/Linux is from the -s parameter.
In the Linux Shell Introduction, you saw a few environment variables. The variables were $SHELL and $HISTSIZE. Now we will cover the rest of these environment variables.
First, let's look at what the environment is in the Operating System (OS). Each program which runs is called a process. Each process is run within an environment. The environment consists of the input and output files. Input files are those needed by the program such as library files and the like. Examples of output files are saved data and temporary files. Each process also contains the Environment Variables which are configurable options to control the environment.
The environment is easily controlled within a shell. Each command you enter into the shell may create a new process. The programs you execute from a shell create a new process, but some commands may only change the shell such as 'ls'. The example command 'ls' displays a list of the directories and files within the current directory location.
There are five possible states for each process:
- Waiting – The program is loaded from storage into memory. This is the initial state of any process. Once loaded, the process is in a waiting state.
- Running – While waiting, the scheduler will assign time for the process to be processed by the Central Processing Unit (CPU). Once the code is processed and executed, it is considered in the running state.
- Blocked – Any time the process needs to wait for a resource to become available, it is in a blocked state. Once the resource is available, the process returns to a waiting state.
- Terminated – When a program completes or is closed, the state changes to terminated. One at this state, the process remains until it can be scheduled to be removed from memory.
- Zombie – When a process creates a child process and then the parent process crashes or is killed, the child process continues and termed a zombie process.
SSH_AGENT_PID=1985
DEVTYPE=drm_minor
SHELL=/bin/bash
TERM=xterm
KERNEL=card0
XDG_SESSION_COOKIE=d18aaa552dc2704636b4997c0000000a-1402844511.879491-1867820031
SUBSYSTEM=drm
E_DATA_DIR=/usr/share/enlightenment
GNOME_KEYRING_CONTROL=/home/jarret/.cache/keyring-GzDy5Z
E_START_TIME=1402844512.8
PRIMARY_DEVICE_FOR_DISPLAY=1
DEVPATH=/devices/pci0000:00/0000:00:01.0/0000:01:05.0/drm/card0
USER=jarret
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00



XTERM_256_COLORS=1
E_BIN_DIR=/usr/bin
TAGS=:udev-acl:
E_SCALE=1.200
SSH_AUTH_SOCK=/tmp/ssh-pGlwusaj1947/agent.1947
DEFAULTS_PATH=/usr/share/gconf/Enlightenment.default.path
E_ICON_THEME=MaXo-ReMix
XDG_CONFIG_DIRS=/etc/xdg/xdg-Enlightenment:/etc/xdg
MINOR=0
E_LIB_DIR=/usr/lib
PATH=/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
DESKTOP_SESSION=Enlightenment
ACTION=add
RUNLEVEL=2
JOB=udev-fallback-graphics
PWD=/home/jarret
UDEV_LOG=3
RESULT=ok
LANG=en_US.UTF-8
GNOME_KEYRING_PID=1871
TERMINOLOGY=1
MANDATORY_PATH=/usr/share/gconf/Enlightenment.mandatory.path
PREVLEVEL=N
MAJOR=226
USEC_INITIALIZED=14856703
E_RESTART=1
PANTS=ON
E_LOCALE_DIR=/usr/share/locale
E_CONF_PROFILE=Fancy
DEVNAME=/dev/dri/card0
HOME=/home/jarret
SHLVL=1
XORGCONFIG=/etc/X11/xorg.conf
UPSTART_INSTANCE=
UPSTART_EVENTS=filesystem runlevel started drm-device-added stopped
LOGNAME=jarret
E_PREFIX=/usr
E_START=/usr/bin/enlightenment_start
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-AGuUZ6VYq4,guid=5d0fb7dfbc6bc601bb12c8fd00000026
XDG_DATA_DIRS=/usr/share/enlightenment:/usr/share:/usr/share/Enlightenment:/usr/local/share/:/usr/share/
LESSOPEN=| /usr/bin/lesspipe %s
DESKTOP=Enlightenment-0.17.0
UPSTART_JOB=lxdm
INSTANCE=
DISPLAY=:0.0
LESSCLOSE=/usr/bin/lesspipe %s %s
E_IPC_SOCKET=/tmp/e-jarret@0/:0.0-1990
XAUTHORITY=/home/jarret/.Xauthority
SEQNUM=1538
_=/usr/bin/env
Some of these Environment Variables are used by the OS and each distro can vary. There are Environment Variables that are important to know. These variables are as follows:
- HOME – Specifies the current user's HOME directory
- LOGNAME – The username which is associated with the logged in user
- MAIL – Contains the path to the current user's mailbox
- OLDPWD – The previous directory you were in
- PATH – Directories through which the shell searches for entered commands
- PS1 – Shell prompt
- PWD – contains the current directory for which you are located
- SHELL – identifies location and shell type
- TERM – Sets name of terminal type
- TZ – Identifies the time zone for your system
- DESKTOP – Name of the windows manager being used, such as Enlightenment
- DESKTOP_SESSION – Name of the currently active windows manager
- LANG – specifies software language such as 'en_US.UTF-8'
- RUNLEVEL – Displays current Runlevel
- LS_COLORS – Shows default colors used by the 'ls' command for files, directories, etc.
To display any of these variables for your system, go to a Terminal and type 'echo $VARIABLE'. For example, to find my Home directory I type 'echo $HOME' and the result is '/home/jarret'.
NOTE: The command 'echo' is used to display what is followed by the command. The dollar sign is used to signify that the contents of the variable are to be displayed. Without the dollar sign, the typed word would be printed back.
To set an Environment Variable, you can do one of the following exports:
- export NAME=VALUE
- NAME=VALUE; export NAME
For example, let's look at my existing PATH variable. It contains the values '/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games'. If a new directory was made which contained program files I wrote and wished to use in a shell, I would need to add it to the PATH. Let's assume the directory is /home/jarret/commands/. To add this directory to the PATH variable would require one of the following commands:
- export PATH= $PATH:”/home/jarret/commands”
- PATH=$PATH:”/home/jarret/commands” export PATH
NOTE: Note the strings are enclosed in quotes.
By adding the $PATH as part of the new PATH name, it includes the existing path so there is no need to retype all of the paths. If you want to type it all in and remove entries, then you would not use the $PATH. For example, to remove 'usr/games' from the original PATH, I would perform one of the following:
- export PATH=”/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”
- PATH=”/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”;export path
NOTE: Each directory is separated by a colon

Two very important variables to know are the PS1 and UNAME variables since they can require parameters.
The PS1 variable controls the Shell prompt. The default prompt for my system is; \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$. This seems like a lot of gibberish, but it will make sense soon.
The parameters for the PS1 variable are as follows:
- \\ – represents a single backslash
- \[ – begins a sequence of characters which are not printed
- \] – ends a sequence of characters which are not printed
- \# – number of a command
- \! – history number of command
- $ – shows the user is root
- \d – date format in 'Day-of-Week Month Date' such as Sun June 15
- \h – host name
- \n – new line
- \nnn – character for octal number given by nnn
- \s – name of shell
- \t – current time in the format of HH:MM:SS
- \u – name of logged in user
- \w – name current directory
- \W – base name of current directory
If we look back at my PS1 variable contents, we see the following:
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$. Let's remove the ANSI color codes first and get: '${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '. Here, the {debian_chroot:+($debian_chroot)}is used when a chroot is set and you change roots. Most people do not use the chroot, so for now we can ignore this. After the chroot information we see \u@\h:\w\$. This shows the shell prompt will be the username@hostname:directory$. All parameters must begin with a \ or it is taken as a literal character which will be printed on the screen.
The ANSI color codes are as follows:
- Foreground
- [30m – Black
- [31m – Red
- [32m – Green
- [33m – Yellow
- [34m – Blue
- [35m – Magenta
- [36m – Cyan
- [37m – White
- [39m – Use system default
- Background
- [40m – Black
- [41m – Red
- [42m – Green
- [43m – Yellow
- [44m – Blue
- [45m – Magenta
- [46m – Cyan
- [47m – White
- [49m – Use system default
To use the ANSI codes you start with a \033[01;##m\]. Looking back at my sample PS1, you can see that the user and hostname are green. The directory is blue. Using the [00m\] causes the color to return to default (same as [01;39m\]). To change the username and hostname to magenta and the directory to cyan, the following code would be used:
export PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;35m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w[\033[00m\]\$"
NOTE: The long string used to create the prompt is enclosed in quotes.
To add a background color, add \[033[01;47m\] after the foreground color and at the end where it should stop, add \[033[01;49m\].
The shell command 'uname' is used more for getting system information. This information cannot be changed only viewed. By typing uname in the shell, I get back the following:
Linux
This shows I am using a Linux OS. The parameters for uname are:
- -a – all options are printed
- -m – print machine hardware type
- -n – displays hostname
- -r – lists operating system's release name
- -s – prints operating system's name (default when no parameters are entered)
- -v – displays operating system's kernel build
Linux jarret-Presario-CQ62-Notebook-PC 3.8.0-19-generic #30~precise1-Ubuntu SMP
Wed May 1 22:26:36 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
The jarret-Presario-CQ62-Notebook-PC is for the -n parameter. The 3.8.0-19-generic is for the -r parameter which is the release version. The -v displays the kernel build which in this case is the #30~precise1-Ubuntu SMP Wed May 1 22:26:36 UTC 2013 message. The -m produces the architecture: x86_64. The OS is GNU/Linux is from the -s parameter.