Mozilla Skin



Tabla de contenidos

Shells and Shell Scripts

  • A shell is a program which reads and executes commands for the user.
  • Shells also usually provide features such job control, input and output redirection and a command language for writing shell scripts.
  • A shell script is simply an ordinary text file containing a series of commands in a shell command language (just like a "batch file" under MS-DOS).
  • There are many different shells available on UNIX systems, and they each support a different command language.
  • Here we will discuss the command language for the Bourne shell sh since it is available on almost all UNIX systems.

Shell Variables and the Environment


  • A shell lets you define variables (like most programming languages).
  • A variable is a piece of data that is given a name. Once you have assigned a value to a variable, you access its value by prepending a
$ to the name
$ bob='hello world'
$ echo $bob
hello world

Variables and the shell

  • Variables created within a shell are local to that shell, so only that shell can access them.
  • The set command will show you a list of all variables currently defined in a shell.
  • If you wish a variable to be accessible to commands outside the shell, you can export it into the environment:
$ export bob

Variables and the environment

  • The environment is the set of variables that are made available to commands (including shells) when they are


  • UNIX commands and programs can read the values of environment variables, and adjust their behavior accordingly.
  • For example, the environment variable PAGER is used by the man command (and others) to see what command should be used to display multiple pages. If you say:
$ export PAGER=cat
  • and then try the man command (say man pwd), the page will go flying past without stopping. If you now say:
$ export PAGER=less
  • normal service should be resumed (since now more will be used to display the pages one at a time).

  • An interesting environment variable is PS1, the main shell prompt string which you can use to create your own custom prompt.
$ echo $PS1
  • Try
$ export PS1="(\h) \w> "

The following list shows the meanings of the special characters used to define the PS1

  • \t - time
  • \d - date
  • \n - newline
  • \s - Shell name
  • \W - The current working directory
  • \w - The full path of the current working directory.
  • \u - The user name
  • \h - Hostname
  • \# - The command number of this command.
  • \! - The history number of the current command

PATH Variable

Another important environment variable is PATH. PATH is a list of directories that the shell uses to locate executable filesfor commands.

$ echo $PATH

So if the PATH is set to:


and you typed ls, the shell would look for /bin/ls, /usr/bin/ls etc. Note that the PATH contains'.', i.e. the current working directory. This allows you to create a shell script or program and run it as a command from your current directory without having to explicitly say "./filename".