Introduction to Components of a Shell Program
The previous module introduced you to many of the available programming tools and explained when shell scripts are the best tool to solve a problem.
This module provides more information to help you understand the commands and tools you will be using to create your first shell script. By the end of this module, you will be able to:
- Explain situations in which a shell script is most useful
- Define external and built-in commands
- List commands used to communicate with users
- Identify how tests are used within shell scripts
- Define the different variable types used in shell scripts
- Describe how control structures determine how a script operates
Linux Shell Scripting
Berkeley C shell
Because the Berkeley C shell (csh) offered features that were more pleasant for interactive use,
such as command history and job control, for a long time the standard practice in the Unix world was to use the Bourne shell for programming and the C shell for daily use. David Korn at Bell Labs was the first developer to enhance the Bourne shell by adding csh-like features to it:
- job control, and
- additional programmability.
Eventually, the Korn shell's feature set surpassed both the Bourne shell and the C shell, while remaining compatible with the Bourne shell
for shell programming. Today, the POSIX standard defines the standard shell
language and behavior based on the System V Bourne shell, with a selected subset of features from the Korn shell.
Ever since the early days of computers, shell scripts have been helping systems administrators and programmers perform tedious jobs that otherwise
took time. So what is a shell script, and why should you care? Shell scripts are text files that run a set of commands, in the order they are written in the script, for a particular shell (in our case, bash). The shell is your command line interface to the library of commands available on your operating system. Shell scripts are essentially bite-sized programs built using the commands available in your shell environment to automate specific tasks, generally those tasks that no one enjoys doing by hand, like web scraping, tracking disk usage, downloading weather data, renaming files, and much more.
You can even use shell scripts to make basic games.
These scripts can include simple logic, like the if statements you may have seen in other languages, but they can also be even simpler, as you will soon see.
Many flavors of command line shells, such as tcsh, zsh, and the everpopular bash, are available for OS X, BSD, and Linux operating systems.
This module will focus on the mainstay of the Unix environment, bash. Each shell has its own features and capabilities, but the shell that most people first become familiar with on Unix is usually bash.
On OS X, the Terminal app will open a window with a bash shell.
On Linux, the command shell program can vary widely, but common command line consoles are gnome-terminal for GNOME or konsole for KDE. These applications
can have their configurations changed so that they use different types of command line shells, but they all use bash by default.
Essentially, if you are on any kind of Unix-like operating system, opening the terminal application should present you with a bash shell by default.