Introduction

In Serious Shell Programming you will learn how to write professional looking, feature-rich command-line programs that run on Apple Mac OS™ X, Linux, FreeBSD, and even Microsoft Windows™ as well as many others.

The command-line is an optional interface on many Operating Systems (OS), including Mac OS™ and Windows™. If your OS does not provide a Graphical User Interface (GUI), the main interface is the Command-Line Interface (CLI) or sometimes referred to as the console. Even if your OS presents a GUI by default, such as Mac OS™ and Window™, there may be an underlying or optional CLI. Usually a terminal program is included to give access to the CLI from within the GUI and there may be many different terminal programs available for your OS.

At the heart of any CLI, whether running directly on the console or in a terminal window, is the shell. One responsibility of the shell is interacting with users on the command-line while another is running so-called shell scripts. Shell scripts are programs written in the shell language, allowing you to control the command-line in an automated fashion. There are many types of shells, some that specialize in user interaction, others in scripting language they offer, and yet others that handle both well.

Not all shells speak the same language and not all shells are available on all Operating Systems. For example, writing a script that uses the Z shell (zsh) may not be compatible with your friends system which lacks zsh. These are known as portability issues and choosing your shell carefully can improve portability to multiple Operating Systems.

All currently-maintained UNIX/Linux Operating Systems provide a shell at /bin/sh. Despite the amalgam of different versions out there, every shell found at /bin/sh must implement at-minimum the POSIX standard. The implementation of which strictly defines the base functionality that must be present for programmers. While shell maintainers are free to add features that go above and beyond the POSIX standard, targeting the core POSIX functionality allows maintainers to present you with a common interface, portable to many similar environments.

On Linux, /bin/sh is often a version of the Bourne Again Shell (bash) while on FreeBSD and many versions of UNIX /bin/sh does not support the higher-level features of bash.

Writing code that targets the baseline POSIX features of /bin/sh maximizes portability and usually is done with a specific purpose in-mind. Code that runs in a restricted environment with few dependencies, for example, can benefit from using strict POSIX /bin/sh code in the event more capable languages such as Python or Perl may not be available.

The techniques in this book will help you create shell scripts that are compatible with FreeBSD, NetBSD, Solaris, and Linux including RedHat, CentOS, Ubuntu, and many more. Specifically avoided are unportable techniques, especially those that only work on systems that use bash installed to /bin/sh.