Linux+: Linux Shell 23 – Environment Variables

J

Jarret W. Buse

Guest
Linux+: Linux Shell 23 – Environment Variables

Many programs that run on Linux systems make use of Environment Variables. The variables are set by Linux or even the applications when installed or started for the first time. Environment variables can be used to specify any value, usually color codes or file locations.

The best example is when a suite of programs are used and they each require various settings which are used by all applications. Some programmers can place the information in a common file which stores the configuration information. In some cases, this can cause issues if one program is saving changed information and another is starting up which needs to load the information from the file. The answer is to use Environment Variables to prevent such an issue. Originally, the variables were used to configure the Operating System (OS) environment. For example, the variable TEMP contained the location of the TEMP directory where temporary files could be created and stored until the system was shut down. Another example is the PATH variable to specify the folders which should be searched when a command is entered.

In BASH, to set an Environment Variable you use the following syntax:

  1. name=string; export name
  2. export name=string
The “name” specifies the Environment Variable while “string” denotes what it stands for when the variable name is used.

NOTE: The Environment Variable name is case-sensitive when used by programs or scripts.

When used in scripts and programs, the Environment Variable is preceded by a dollar sign ($).

A Variable can be set within a shell and is stored until the system is shutdown. To keep the variable persistent they need to be set in the ~/.profile file.

When setting a variable, such as PATH, you can set the variable as follows:

PATH= ~/:/bin:/usr/bin; export PATH

The command will overwrite the existing PATH variable to the new one. What if you want to add the previous folders to the existing PATH? You do the following:

PATH= $PATH:~/:/bin:/usr/bin; export PATH

The “$PATH” variable with the dollar sign ($) is the existing value of the variable. The string following the “$PATH” is added to the existing PATH variable contents.

To view the values of any variable, type:

echo $name

For example, to see the contents of TEMP, type:

echo $TEMP

To see all the Environment Variables set on a system use the following:

printenv

The command is to “print environment”. All variables will be listed and can be piped through the more or less command to see one screen at a time.

Some standard Environment Variables are:

  • HOME – Home directory of current user
  • LOGNAME – Login name of current user
  • MAIL – Sets path to current user's mailbox
  • OLDPWD – Previous directory (Old Previous Working Directory)
  • PATH – Directories in which the shell should look for commands
  • PS1 – Shell prompt default
  • PWD – Current location in the file system (Previous Working Directory)
  • SHELL – specifies location of the shell executable
  • TERM – Sets terminal type
  • TZ – Identifies time zone
There are two Environment Variables which are used to help the shell user for prompts. These two variables are PS1 and PS2. It should be noted that PS3 and PS4 do exist, but the first two are more used.

The PS1 variable is used to set the default prompt for the shell. For example, my shell has the prompt “jarret@jarret-Presario-CQ62-Notebook-PC ~ $”. The PS1 variable is “${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\]”.

The values have the following meaning:

  • \d – Date (Day Month Date) (Sat Nov 8)
  • \h – Hostname up to first period (.)
  • \H – Full hostname
  • \n – Line feed
  • \nnn – Character represented by the octal number nnn
  • \s – Shell name (BASH)
  • \t – Current time (HH:MM:SS) (11:34:23)
  • \u – Username of current logged in user
  • \w – Current working directory, full path
  • \W – Current working directory, current folder
  • \# – Command number
  • \! – History number
  • \\ – Prints a backslash
  • \[ – Begins a sequence of non-printing characters
  • \] – Ends a sequence of non-printing characters
  • $ – Prompt ends in a $ unless the user is logged in as Root
A short example is to change the prompt to Uername:Hostname:Full_working_directory $. The code would be: PS1= [/u:/h:/w] $.

The PS2 prompt variable is used to specify what prompt is used when a line is continued. To continue a line to the next, use a backslash (\) at the end and press enter. The PS2 prompt is displayed at the beginning of the line to indicate it is continued. The default is a greater than symbol (>).

The PS3 prompt variable is used to specify the prompt used when a select command is used. A select command produces a list and waits for a user input.

The PS4 prompt variable specifies the information displayed when the shell is used in debug mode.

One final variable is the COMMAND_PROMPT variable. This variable is similar to the PS1 variable using the same context. The COMMAND_PROMPT is displayed before the PS1 prompt.

It is best to understand the use of Environment Variables in a shell. The ability to customize the shell prompt can be a very handy one to use.
 

Attachments

  • slide.jpg
    slide.jpg
    49.4 KB · Views: 31,128

Members online


Latest posts

Top