LL9 – Linux Variables

Introduction

As before, you are responsible for all previous Linux coursework and for researching the commands below in more depth using Wikipedia and the built-in Linux man pages.  You will also be taking screen shots, pasting them into your LastnameFirstnameLL9.odt file and submitting (uploading) them in BB.

9.1 Linux Variables

Variables are a way of passing information from the shell to programs when you run them. Programs look “in the environment” for particular variables and if they are found, they will use the accessible stored values. Some variables are set by the system, others are set by you, yet others are set by the shell, or by any program that loads another program.

Standard Linux variables are split into two categories, environment variables and shell variables. In broad terms, shell variables apply only to the current instance of the shell and are used to set short-term working conditions; environment variables have a farther reaching significance, and those set at login are valid for the duration of the session. By convention, environment variables have UPPER CASE names and shell variables have lower case names.

9.2 Environment Variables

An example of an environment variable is the OSTYPE variable. The value of this is the current operating system you are using. Type

$ echo $OSTYPE #note you should be looking up echo

More examples of environment variables are as follows:

USER (your login name)

HOME (the path name of your home directory)

HOST (the name of the computer you are using)

ARCH (the architecture of the computers processor)

DISPLAY (the name of the computer screen to display X windows)

PRINTER (the default printer to send print jobs)

PATH (the directories the shell should search to find a command)

Finding out the current values of these variables.

ENVIRONMENT variables are set using the setenv command, displayed using the printenv or env commands, and unset using the unsetenv command.

To show all values of these variables, type

$ printenv | less

Exercise 9a

Take a screenshot and paste this into your .odt document.

9.3 Shell Variables

An example of a shell variable is the history variable. The value of this is how many shell commands to save, allowing the user to scroll back through all the commands they have previously entered. First type history to see its application:

$ history

Now lets look at the history variables size (i.e. how many commands it will store)

$ echo $HISTSIZE

Now let’s use history to find out something really useful like what “sudo” commands have we entered using a pipe and grep

$ history | grep sudo

Now let’s get rid of duplicates from our history display by modifying the varialble

$ export HISTCONTROL=ignoredups #try the history command again to verify

More examples of shell variables are

cwd (your current working directory)

home (the path name of your home directory)

path (the directories the shell should search to find a command)

prompt (the text string used to prompt for interactive commands shell your login shell)

Finding out the current values of these variables.

SHELL variables are both set and displayed using the set command. They can be unset by using the unset command.

To show all values of these variables, type

$ set | less #use ‘q’ to break out of this process after 1 screen of variables

Exercise 9b

Take a screenshot and paste this into your .odt document.

So what is the difference between PATH and path ?

In general, environment and shell variables that have the same name (apart from the case) are distinct and independent, except for possibly having the same initial values. There are, however, exceptions.

Each time the shell variables home, user and term are changed, the corresponding environment variables HOME, USER and TERM receive the same values. However, altering the environment variables has no effect on the corresponding shell variables.

PATH and path specify directories to search for commands and programs. Both variables always represent the same directory list, and altering either automatically causes the other to be changed.

9.4 Using and setting variables

Each time you login to a Linux host, the system looks in your home directory for initialisation files. Information in these files is used to set up your working environment. When we open a terminal, our bash shell uses/executes 2 files called .bashrc and .profile. located in your home directory (note that both file names begin with a dot). Take a look at the contents of these files using the tool of your choice (cat, more, less, etc.).  If you use an editor (e.g. vim) to view the contents of these files – DO NOT CHANGE ANYTHING!!! Laughing

Modifying these files will be one of the soon to be developed final projects.

Leave a Reply