An Introduction to the UNIX Shell
S. R. Bourne · 2008
The is a command programming language that provides an interface to the UNIX® operating system. Its features include control-flow primitives, parameter passing, variables and string substitution. Constructs such as while, if then else, case and for are available. Two-way communication is possible between the and commands. String-valued parameters, typically file names or flags, may be passed to a command. A return code is set by commands that may be used to determine control-flow, and the standard output from a command may be used as input. The can modify the environment in which commands run. Input and output can be redirected to files, and processes that communicate through ‘pipes’ can be invoked. Commands are found by searching directories in the file system in a sequence that can be defined by the user. Commands can be read either from the terminal or from a file, which allows command procedures to be stored for later use. 1.0 Introduction The is both a command language and a programming language that provides an interface to the operating system. This memorandum describes, with examples, the shell. The first section covers most of the everyday requirements of terminal users. Some familiarity with is an advantage when reading this section; see, for example, UNIX for beginners. Section 2 describes those features of the primarily intended for use within procedures. These include the control-flow primitives and string-valued variables provided by the shell. A knowledge of a programming language would be a help when reading this section. The last section describes the more advanced features of the shell. References of the form see pipe (2) are to a section of the manual. 1.1 Simple commands Simple commands consist of one or more words separated by blanks. The first word is the name of the command to be executed; any remaining words are passed as arguments to the command. For example, who is a command that prints the names of users logged in. The command ls −l † This paper was updated in 2010 to reflect most features of modern POSIX shells, which all follow the design of S.R. Bourne’s original v7 Unix shell. Among these are ash, bash, ksh and others. Typically one of these will be installed as /bin/sh on a modern system. It does not describe the behavior of the c (csh). If it’s the c (csh) you’re interested in, a good place to begin is William Joy’s paper Introduction to the C shell (USD:4). USD:3-2 An Introduction to the Shell prints a list of files in the current directory. The argument −l tells ls to print status information, size and the creation date for each file. 1.2 Input output redirection Most commands produce output on the standard output that is initially connected to the terminal. This output may be sent to a file by writing, for example, ls −l >file The notation >file is interpreted by the and is not passed as an argument to ls. If file does not exist then the creates it; otherwise the original contents of file are replaced with the output from ls. Output may be appended to a file using the notation ls −l >>file In this case file is also created if it does not already exist. The standard input of a command may be taken from a file instead of the terminal by writing, for example,