Thompson shell

Last updated
Thompson shell
Original author(s) Ken Thompson
Developer(s) AT&T Bell Laboratories
Initial releaseNovember 3, 1971;51 years ago (1971-11-03)
Operating system Unix and Unix-like
Type Unix shell

The Thompson shell was the first Unix shell, introduced in the first version of Unix in 1971, and was written by Ken Thompson. [1] It was a simple command interpreter, not designed for scripting, but nonetheless introduced several innovative features to the command-line interface and led to the development of the later Unix shells.

Contents

History

The name "shell" for a command-line interpreter and the concept of making the shell a user program outside of the operating system kernel were introduced in Unix's precursor Multics.

An early feature of the Thompson shell was a compact syntax for input/output redirection. In Multics, redirecting the input or output of a command required separate commands to start and stop redirection; in Unix, one could simply add an argument to the command line consisting of the < symbol followed by a filename for input or the > symbol for output, and the shell would redirect I/O for the duration of the command. This syntax was already present by the release of the first version of Unix in 1971.

A later addition was the concept of pipes. At the suggestion of Douglas McIlroy, the redirection syntax was expanded so that the output of one command could be passed to the input of another command. The original pipe syntax, as described in the Version 3 manual, was:

command1 >command2>

This syntax proved too ambiguous and was easily confused with redirection to and from files—the system cannot tell if "command2" is the command "command2" or the file "command2". By Version 4, the syntax had changed to use both the | and ^ symbols to denote pipes:

command1 | command2

This produces exactly the same result as:

command1 ^ command2

The > symbol changed into:

command1 > file1

This would put the output of command1 into file1.

The Thompson shell syntax for redirection with < and >, and piping with |, has proven durable and has been adopted by most other Unix shells and command shells of several other operating systems, most notably on DOS, OS/2 and Microsoft Windows.

Design

The shell's design was intentionally minimalistic; even the if and goto statements, essential for control of program flow, were implemented as separate commands. [1]

The shell has no facilities for comments besides a builtin command :. Programmers simply write text after this command, which ignores all parameters and simply succeeds. Other builtins include chdir, exit, login, newgrp, shift, and wait. [2]

The if command combines the uses of modern-day Bourne shell test and if. The command first looks for an expression (which can be similar to modern-day test or involve an external command) then treats the rest of the command-line as the command to execute if the condition turns out true. There is no else branch. [3]

goto is implemented in an interesting way, as it is separate from the shell. When asked to jump to "LABEL", it seeks the current command file for a line that says : LABEL (recall that : is simply ignored), then exits. When the shell tries to read a next line, the repositioned file descriptor will direct it to the labelled location. [4]

There is no redirection of additional file descriptors other than standard input and output (0 and 1) in Thompson shell. Redirection of stderr (file descriptor 2) also requires an external program wrapper, fd2. [5]

The shell supports globbing, [2] but actually implements it by deferring it to a glob command that replaces the arguments and calls the requested command. [6]

Thompson shell has positional parameters, but no named variables nor access to environmental variables. It understands the creation of background commands with &, similar to Bourne shell. It offers quoting and backslash escapes, though the single quotes work differently from Bourne shell. [2]

Decline and replacements

As a result of the simplistic design, by the 1975 release of Version 6 Unix, it was becoming clear that the Thompson shell was inadequate for most serious programming tasks.

At this time, the developers of the Programmer's Workbench UNIX distribution, most notably John Mashey, began modifying the Thompson shell to make it more suitable for programming. [1] The result, known as the PWB shell or the Mashey shell, included more advanced flow-control mechanisms and introduced shell variables, but remained limited by the necessity to remain compatible with the Thompson shell.

Finally, the Thompson shell was replaced as the main Unix shell by the Bourne shell in Version 7 Unix and the C shell in 2BSD, both released in 1979. Since virtually all modern Unix and Unix-like systems are descended from V7 and 2BSD, the Thompson shell is generally no longer used. It is, however, available as open-source as part of several Ancient Unix source distributions, and has been ported to modern Unices as a historical exhibit.

See also

Related Research Articles

<span class="mw-page-title-main">Bash (Unix shell)</span> GNU replacement for the Bourne shell

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions. Bash was one of the first programs Linus Torvalds ported to Linux, alongside GCC. A version is also available for Windows 10 and Windows 11 via the Windows Subsystem for Linux. It is also the default user shell in Solaris 11. Bash was also the default shell in versions of Apple macOS from 10.3 to 10.15, which changed the default shell to zsh, although Bash remains available as an alternative shell.

<span class="mw-page-title-main">Unix shell</span> Command-line interpreter for Unix operating system

A Unix shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and is used by the operating system to control the execution of the system using shell scripts.

<span class="mw-page-title-main">Bourne shell</span> Command-line interpreter for operating systems

The Bourne shell (sh) is a shell command-line interpreter for computer operating systems.

pwd Directory information command on various operating systems

In Unix-like and some other operating systems, the pwd command writes the full pathname of the current working directory to the standard output.

In computer programming, standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin), standard output (stdout) and standard error (stderr). Originally I/O happened via a physically connected system console, but standard streams abstract this. When a command is executed via an interactive shell, the streams are typically connected to the text terminal on which the shell is running, but can be changed with redirection or a pipeline. More generally, a child process inherits the standard streams of its parent process.

In computer programming, glob patterns specify sets of filenames with wildcard characters. For example, the Unix Bash shell command mv *.txttextfiles/ moves all files with names ending in .txt from the current directory to the directory textfiles. Here, * is a wildcard standing for "any string of characters except /" and *.txt is a glob pattern. The other common wildcard is the question mark (?), which stands for one character. For example, mv?.txtshorttextfiles/ will move all files named with a single character followed by .txt from the current directory to directory shorttextfiles, while ??.txt would match all files whose name consists of 2 characters followed by .txt.

<span class="mw-page-title-main">Redirect (computing)</span> Form of interprocess communication

In computing, redirection is a form of interprocess communication, and is a function common to most command-line interpreters, including the various Unix shells that can redirect standard streams to user-specified locations.

In computing, echo is a command that outputs the strings that are passed to it as arguments. It is a command available in various operating system shells and typically used in shell scripts and batch files to output status text to the screen or a computer file, or as a source part of a pipeline.

<span class="mw-page-title-main">Pipeline (Unix)</span>

In Unix-like computer operating systems, a pipeline is a mechanism for inter-process communication using message passing. A pipeline is a set of processes chained together by their standard streams, so that the output text of each process (stdout) is passed directly as input (stdin) to the next one. The second process is started as the first process is still executing, and they are executed concurrently. The concept of pipelines was championed by Douglas McIlroy at Unix's ancestral home of Bell Labs, during the development of Unix, shaping its toolbox philosophy. It is named by analogy to a physical pipeline. A key feature of these pipelines is their "hiding of internals". This in turn allows for more clarity and simplicity in the system.

In computing, a here document is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file. The term is also used for a form of multiline string literals that use similar syntax, preserving line breaks and other whitespace in the text.

The PWB shell was a Unix shell.

<span class="mw-page-title-main">Shell (computing)</span> Computer program that exposes an operating systems services to a human user or other programs

In computing, a shell is a computer program that exposes an operating system's services to a human user or other programs. In general, operating system shells use either a command-line interface (CLI) or graphical user interface (GUI), depending on a computer's role and particular operation. It is named a shell because it is the outermost layer around the operating system.

In computing, tee is a command in command-line interpreters (shells) using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. It is primarily used in conjunction with pipes and filters. The command is named after the T-splitter used in plumbing.

test is a command-line utility found in Unix, Plan 9, and Unix-like operating systems that evaluates conditional expressions. test was turned into a shell builtin command in 1981 with UNIX System III and at the same time made available under the alternate name [.

sort (Unix) Standard UNIX utility

In computing, sort is a standard command line program of Unix and Unix-like operating systems, that prints the lines of its input or concatenation of all files listed in its argument list in sorted order. Sorting is done based on one or more sort keys extracted from each line of input. By default, the entire input is taken as sort key. Blank space is the default field separator. The command supports a number of command-line options that can vary by implementation. For instance the "-r" flag will reverse the sort order.

In Unix and Unix-like operating systems, job control refers to control of jobs by a shell, especially interactively, where a "job" is a shell's representation for a process group. Basic job control features are the suspending, resuming, or terminating of all processes in the job/process group; more advanced features can be performed by sending signals to the job. Job control is of particular interest in Unix due to its multiprocessing, and should be distinguished from job control generally, which is frequently applied to sequential execution.

The script command is a Unix utility that records a terminal session. It dates back to the 1979 3.0 BSD.

<span class="mw-page-title-main">Command-line interface</span> Computer interface that uses text

A command-line interface (CLI) is a means of interacting with a device or computer program with commands from a user or client, and responses from the device or program, in the form of lines of text. Such access was first provided by computer terminals starting in the mid-1960s. This provided an interactive environment not available with punched cards or other input methods.

In computing, process substitution is a form of inter-process communication that allows the input or output of a command to appear as a file. The command is substituted in-line, where a file name would normally occur, by the command shell. This allows programs that normally only accept files to directly read from or write to another program.

cat (Unix) Unix command utility

cat is a standard Unix utility that reads files sequentially, writing them to standard output. The name is derived from its function to (con)catenate files. It has been ported to a number of operating systems.

References

  1. 1 2 3 J. R. Mashey (1976-10-13). Using a Command Language as a High-Level Programming Language (PDF). 2nd International Conference on Software Engineering. pp. 169–176.
  2. 1 2 3 "tsh(1) (html) - TSH(1) - Manuals - Etsh Project (V6Shell)". etsh.nl.
  3. "if(1) (html) - IF(1) - Manuals - Etsh Project (V6Shell)". etsh.nl.
  4. "goto(1) (html) - GOTO(1) - Manuals - Etsh Project (V6Shell)". etsh.nl.
  5. "fd2(1) (html) - FD2(1) - Manuals - Etsh Project (V6Shell)". etsh.nl.
  6. "glob(1) (html) - GLOB(1) - Manuals - Etsh Project (V6Shell)". etsh.nl.