UNIX Shells Review

UNIX Shells: Introduction and Concepts
Shell Description
  • The shell is a UNIX program you interact with, typically by typing commands at a prompt.

  • It functions as a command-line interpreter, acting as an interface between the user and the operating system's kernel.

  • Its primary role is to run other UNIX commands and utilities, processing user input and displaying output.

  • The shell provides an environment for executing programs, managing files, and performing system operations.

  • It also allows for automation through shell scripting.

  • In Emacs, the command Meta-X shell\text{Meta-X shell} invokes an interactive shell subprocess within Emacs.

Shell Choices
  • sh: The Bourne Shell. Developed by Stephen Bourne at Bell Labs, it was the original standard UNIX shell, known for its concise scripting language.

  • csh: The C shell. Developed by Bill Joy, designed with a syntax similar to the C programming language, making it popular among programmers, and introduced features like command history and job control.

  • tcsh: The Turbo C Shell, an enhanced version of csh. It adds productivity features such as command-line editing, programmable word completion, and advanced history mechanisms.

  • ksh: The Korn shell. Developed by David Korn, it combines the best features of sh (scripting compatibility) and csh (interactive features like command history and editing), and is POSIX compliant.

  • bash: The Bourne Again Shell, widely used and often the default on Linux and macOS systems. It is a free software replacement for sh, ksh, and csh, incorporating features from all of them, and is also POSIX compliant.

Why Multiple Shells?
  • All shells execute basic UNIX programs (e.g., ls, sort, cp) identically, as these programs are external executables.

  • The differences lie in their interactive features and scripting capabilities:

    • Interactive Features: Different shells offer varying levels of user-friendliness when used interactively, such as:

    • Command History: How commands are stored and recalled (e.g., tcsh and bash have robust history features).

    • Command-line editing: Ability to edit previous commands using arrow keys (prevalent in tcsh, ksh, bash).

    • Alias and Function Definitions: Shortcuts for common commands (e.g., alias ll='ls -al').

    • Job Control: Managing background or suspended processes.

    • Filename Completion (Tab Completion): Automatically completing filenames or commands by pressing the tab key.

    • Scripting Capabilities: While sh provides basic scripting syntax, more advanced shells like bash and ksh offer:

    • Advanced Flow Control: More sophisticated if/else, for, while loops.

    • Array Variables: Handling lists of data.

    • Improved Parameter Expansion: More powerful ways to manipulate string variables.

    • Different Syntax: csh and tcsh have a C-like syntax for scripting, which can be preferred by C programmers but is generally less portable than sh/bash scripts.