Tutorial Eight

 

Sections in this tutorial: 8.1 | 8.2 | 8.3 | 8.4 | 8.5 | Command Summary

Commands in this tutorial: alias | echo | export | gedit | printenv | source

8.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 will use the values stored. Some are set by the system, others by you, yet others by the shell, or 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 and shell variables have lower case names.

8.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

More examples of environment variables are

  • $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

8.3 Using and setting variables

Each time you login to a Linux host, the system looks in your home directory for initialization files. Information in these files is used to set up your working environment. The bash uses a file called .bashrc (note the file name begins with a dot).

At login the Bourne Again shell reads and executes the .bashrc. The .bashrc file is used to set conditions which will apply to the whole session and to perform actions that are relevant only at login as well as set conditions and perform actions specific to the shell and to each invocation of it.

WARNING: NEVER put commands that run graphical displays (e.g. a web browser) in your .cshrc or .login file.

8.4 Setting aliases in the .bashrc file

One of the most common actions to do in the .bashrc file is to set command aliases. The command alias is generally used for two things: setting the "default" for a specific command and making new commands from the set of Bash commands.

For example, if you want your ls command to always do a long listing, you could use alias like this

$ alias ls='ls -l'

Check this has worked by typing

$ ls

Also, if you want a new command, say dir, to do a long listing similar to the old DOS listing, you could use an alias like this

$ alias dir='ls -al'

Check this has worked by typing

$ dir

However, this has only set the variable for the lifetime of the current shell. If you open a new terminal window, it will not have your new aliases set. To PERMANENTLY set the alias values, you will need to add the set commands in the .bashrc file.

First open the .bashrc file in a text editor. An easy, user-friendly editor to use is gedit.

$ gedit ~/.bashrc

Add the following line AFTER the list of other commands at the bottom of the file.

alias dir='ls -al'

Save the file and force the shell to reread the contents of the file buy using the shell source command.

$ source .bashrc

Check this has worked by typing

$ dir

8.5 Setting the path

When you type a command, your path (or PATH) variable defines in which directories the shell will look to find the command you typed. If the system returns a message saying "command: Command not found", this indicates that either the command does not exist at all on the system or it is simply not in your path.

For example, to run units, you either need to directly specify the units path (~/units174/bin/units), or you need to have the directory ~/units174/bin in your path.

You can add it to the end of your existing path (the $PATH represents this) by issuing the command:

$ PATH=~/units174/bin:"${PATH}"
$ export $PATH

Test that this worked by trying to run units in any directory other that where units is actually located.

$ cd; units

HINT: You can run multiple commands on one line by separating them with a semicolon.

To add this path PERMANENTLY, add the following line to your .bashrc AFTER the list of other commands.

PATH=~/units174/bin:"${PATH}"

Summary


alias Define an alias for a command
echo Print out the value of an environment variable
export Define environment variables to the shell
gedit A user friendly editor
printenv Show the values of environment variables
source Run a shell script