Main index

Introducing UNIX and Linux


Introduction to shells

Overview
Why do we need a shell?
Shell syntax
      Types of shell command
      Simple commands
      Pipelines
      Grouping commands
      Exit status
      List commands
Arithmetic
      Operators and functions
Making decisions
      The 'test' statement
            Operators used by 'test'
      The 'if' statement
Loops
      'For' loops
      'While' and 'until' loops
Searching for files
      Arguments to 'find'
Formatted output
      Arguments to 'printf'
Passing information to scripts
      Scripts with arguments
      Parameter expansion
Summary
Exercises

Why do we need a shell?

This question may be on your mind. Most of the commands discussed so far are to be found in directories mentioned in your PATH. You can edit files, print files, and run programs. You can schedule commands. You have access to languages such as Pascal or C on your system, and they can be used for complex programming tasks. So what else does the shell have to offer?

The shell allows you to check on the success and failure of commands, on the state of the filesystem, on the values of environment variables, and to process this knowledge. It is a programming language in the full sense - it has the power of other programming languages - but tailored for use in conjunction with an operating system. It contains built-in features that allow the user to get the maximum amount of information from the kernel in an easy manner. By writing shell scripts you can also create your own commands.

Although the shell is a powerful programming language, it is designed as a user interface to a UNIX machine, and is not an ideal language for doing complex numerical calculations. If you have a particular application that does not clearly have a need to be written in the shell, then it is good practice to write it in another language better suited, and then call that program from the shell.

It is not possible to give exact instructions as to where the boundary lies - when you should decide that the shell is unsuitable and use another language - but the examples in the following chapters will give you a feel as to what sorts of task are typically programmed in the shell. Some people use a UNIX system happily and hardly ever use any of the shell facilities, while others are quite at home with the most complex scripts.


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck