Try DigitalOcean free for up to two months!

Linux+: Linux Shell 02 – Environment Variables

Discussion in 'Shell / Command Line' started by Jarret W. Buse, Jun 22, 2014.

  1. Jarret W. Buse

    Jarret W. Buse Well-Known Member Staff Writer

    Jul 3, 2013
    Likes Received:
    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:

    1. 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.
    2. 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.
    3. 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.
    4. 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.
    5. 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.
    Controlling the Environment Variables is usually not needed, but can come in handy in some instances. Before we continue, you can open a Terminal and type the command 'env'. The 'env' command lists the Environment Variables and their contents, which should be similar to the following:

    UPSTART_EVENTS=filesystem runlevel started drm-device-added stopped
    LESSOPEN=| /usr/bin/lesspipe %s
    LESSCLOSE=/usr/bin/lesspipe %s %s

    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:

    1. HOME – Specifies the current user's HOME directory
    2. LOGNAME – The username which is associated with the logged in user
    3. MAIL – Contains the path to the current user's mailbox
    4. OLDPWD – The previous directory you were in
    5. PATH – Directories through which the shell searches for entered commands
    6. PS1 – Shell prompt
    7. PWD – contains the current directory for which you are located
    8. SHELL – identifies location and shell type
    9. TERM – Sets name of terminal type
    10. TZ – Identifies the time zone for your system
    11. DESKTOP – Name of the windows manager being used, such as Enlightenment
    12. DESKTOP_SESSION – Name of the currently active windows manager
    13. LANG – specifies software language such as 'en_US.UTF-8'
    14. RUNLEVEL – Displays current Runlevel
    15. 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
    NOTE: Changes made within the current shell will only remain until the shell is closed. To make the changes permanent, you need to add the variable to the ~/.bashrc file to affect your shell only. To affect all user shells, change the /etc/bash.bashrc file.

    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:


    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
    By entering 'uname -a' in my shell, I get the following:

    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.

    Attached Files:

    • slide.jpg
      File size:
      49.4 KB
  2. Joel Giedt

    Joel Giedt New Member

    Sep 6, 2014
    Likes Received:
    One little comment. In my line of work (high performance computing), controlling environmental variables is frequently needed. For one thing, in order to build many open source libraries, you have to set environmental variables that point the makefile to the needed dependencies, such as the LAPACK library location. E.g., it might want DIRLAPACK to be set to that path. Another use is for cross compilation in CUDA and for the Xeon Phi. In both cases LD_LIBRARY_PATH has to be set to the location of the shared libraries in order for the program to run without errors. Finally, behavior of OpenMP programs can be controlled in important ways by manipulating variables such as OMP_NUM_THREADS. So I would say this tutorial ultimately becomes quite valuable!

    ZHANG/ZHIKUN New Member

    Aug 22, 2014
    Likes Received:
    $PATH have such more function! The color schems are more interesting. Abstract concepts has been explained in plain language. thank you.

Share This Page