4.4.1. Variables

We have already seen some examples of the working of the shell, interpreting commands and expanding wildcards.

The shell also has the possibility of using variables. Defining a variable is as follows:

% temp=/tmp/hans

So we define a variable “temp” and give it the value “/tmp/hans”.

We can then use the variable in commands. To use the value of a variable we precede the name with $. With the previous assignment, the following two commands are equivalent.

% cd $temp
% cd /tmp/hans

Notice the difference: the name of the variable is “temp”, the value of the variable (given by $temp) is “/tmp/hans”

With the command echo we can have a look at the value of a variable: echo $temp gives /tmp/hans on the screen.

You can also use a variable in an assignment.

% temp2=$temp/brol

The shell first expands the right side of the equation to “/tmp/hans/brol” and assigns this to the variable “temp2”.

Remember to include the $ if you want to use the value of a variable. The two following commands are not equivalent:

% temp2=$temp/brol
% temp2=temp/brol

It is also possible to do something like temp=$temp/other. First the actual value of “temp” is expanded, “/other” is appended and the whole is assigned to the variable “temp”. The original value of “temp” is overwritten.

In fact, the echo command shows how the shell will interprete the command that follows the echo. For example:

% echo ls *

shows how the shell interpretes this command. This can be usefull if you are not sure how the shell will interprete the command you will use.

It is also possible to store the output of a command into a variable. This is done by enclosing the command with $(...):

% last_line=$(tail -1 /data/tccm/linux_bash/4.2.1/intro.rst)
% echo $last_line
considered usefull for working in the command line environment.
%

There are also some variables that are defined by the shell itself. These are called “environment variables”. Normally their name is in capitals. Some of them are:

  • PATH this is a list of directory names, separated by ‘:’. If you type a command, the shell looks in all these directories in the sequence they are listed, for an executable program with the name of the command. If the command is not found in one of those directories (and is no buildin command), the shell gives an error message:

    % notexist
    -bash: notexist: command not found
    %
    
  • PWD the name of the present working directory

  • USER the name of the user who is executing the shell

  • PS1 defines the prompt

  • HOME the home-directory of the user

The command env shows a list of all environment variables. Just like other variables you can see the value of them using the echo command. You can also redefine them, but this will only be valid for the current session. If you log out and login again, they will have their default value again. Later we will see how we can change them permanently.




Exercises


  1. In a shell script, used to run jobs on our cluster, a temporary directory is defined where the job can dump some files needed during the calculation. Every user has his/her own directory under /temp0. To make the script as general as possible, we prefer to define some variables in the beginning of the file and use them later. To create a new job, only 1 or 2 variables have to be changed to update the script.

    Can you specify a general TEMP variable with the definition of the temporary directory based on the username and the project (a name freely choosen by the user). (The project name can also be used to define in- and output files in a general way.)

  2. Based on the projecti-variable from exercise 1, sometimes an outputfile is defined as $project.out. What is special about this use of the variable ?