.\" %sccs.include.proprietary.roff% .\" .\" @(#)t1 8.1 (Berkeley) 08/14/93 .\" .EH 'USD:3-%''An Introduction to the UNIX Shell' .OH 'An Introduction to the UNIX Shell''USD:3-%' .\".RP .TL An Introduction to the UNIX Shell .AU S. R. Bourne .AI Murray Hill, NJ .AU (Updated for 4.3BSD by Mark Seiden) .AB .LP The .ul shell\(dd .FS \(dd This paper describes sh(1). If it's the c shell (csh) you're interested in, a good place to begin is William Joy's paper "An Introduction to the C shell" (USD:4). .FE is a command programming language that provides an interface to the .UX operating system. Its features include control-flow primitives, parameter passing, variables and string substitution. Constructs such as .ul while, if then else, case and .ul for are available. Two-way communication is possible between the .ul shell 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 shell input. .LP The .ul shell 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. .AE .ds ST \v'.3m'\s+2*\s0\v'-.3m' .SH 1.0\ Introduction .LP The shell is both a command language and a programming language that provides an interface to the UNIX operating system. This memorandum describes, with examples, the UNIX shell. The first section covers most of the everyday requirements of terminal users. Some familiarity with UNIX is an advantage when reading this section; see, for example, "UNIX for beginners". .[ unix beginn kernigh 1978 .] Section 2 describes those features of the shell primarily intended for use within shell 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 \fIpipe\fP (2)" are to a section of the UNIX manual. .[ seventh 1978 ritchie thompson .] .SH 1.1\ Simple\ commands .LP 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, .DS who .DE is a command that prints the names of users logged in. The command .DS ls \(mil .DE prints a list of files in the current directory. The argument \fI\(mil\fP tells \fIls\fP to print status information, size and the creation date for each file. .SH 1.2\ Background\ commands .LP To execute a command the shell normally creates a new \fIprocess\fP and waits for it to finish. A command may be run without waiting for it to finish. For example, .DS cc pgm.c & .DE calls the C compiler to compile the file \fIpgm.c\|.\fP The trailing \fB&\fP is an operator that instructs the shell not to wait for the command to finish. To help keep track of such a process the shell reports its process number following its creation. A list of currently active processes may be obtained using the \fIps\fP command. .SH 1.3\ Input\ output\ redirection .LP 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, .DS ls \(mil >file .DE The notation \fI>file\fP is interpreted by the shell and is not passed as an argument to \fIls.\fP If \fIfile\fP does not exist then the shell creates it; otherwise the original contents of \fIfile\fP are replaced with the output from \fIls.\fP Output may be appended to a file using the notation .DS ls \(mil \*(APfile .DE In this case \fIfile\fP is also created if it does not already exist. .LP The standard input of a command may be taken from a file instead of the terminal by writing, for example, .DS wc file; wc \*(ST ? \*(VT &\|,\fR are called metacharacters. A complete list of metacharacters is given in appendix B. Any character preceded by a \fB\\\fR is \fIquoted\fP and loses its special meaning, if any. The \fB\\\fP is elided so that .DS echo \\\\? .DE will echo a single \fB?\|,\fP and .DS echo \\\\\\\\ .DE will echo a single \fB\\\|.\fR To allow long strings to be continued over more than one line the sequence \fB\\newline\fP is ignored. .LP \fB\\\fP is convenient for quoting single characters. When more than one character needs quoting the above mechanism is clumsy and error prone. A string of characters may be quoted by enclosing the string between single quotes. For example, .DS echo xx\'\*(ST\*(ST\*(ST\*(ST\'xx .DE will echo .DS xx\*(ST\*(ST\*(ST\*(STxx .DE The quoted string may not contain a single quote but may contain newlines, which are preserved. This quoting mechanism is the most simple and is recommended for casual use. .LP A third quoting mechanism using double quotes is also available that prevents interpretation of some but not all metacharacters. Discussion of the details is deferred to section 3.4\|. .SH 1.7\ Prompting .LP When the shell is used from a terminal it will issue a prompt before reading a command. By default this prompt is `\fB$\ \fR'\|. It may be changed by saying, for example, .DS \s-1PS1\s0=yesdear .DE that sets the prompt to be the string \fIyesdear\|.\fP If a newline is typed and further input is needed then the shell will issue the prompt `\fB>\ \fR'\|. Sometimes this can be caused by mistyping a quote mark. If it is unexpected then an interrupt (\s-1DEL\s0) will return the shell to read another command. This prompt may be changed by saying, for example, .DS \s-1PS2\s0=more .DE .SH 1.8\ The\ shell\ and\ login .LP Following \fIlogin\fP (1) the shell is called to read and execute commands typed at the terminal. If the user's login directory contains the file \fB.profile\fP then it is assumed to contain commands and is read by the shell before reading any commands from the terminal. .SH 1.9\ Summary .sp .RS .IP \(bu \fBls\fP .br Print the names of files in the current directory. .IP \(bu \fBls >file\fP .br Put the output from \fIls\fP into \fIfile.\fP .IP \(bu \fBls \*(VT wc \(mil\fR .br Print the number of files in the current directory. .IP \(bu \fBls \*(VT grep old\fR .br Print those file names containing the string \fIold.\fP .IP \(bu \fBls \*(VT grep old \*(VT wc \(mil\fR .br Print the number of files whose name contains the string \fIold.\fP .IP \(bu \fBcc pgm.c &\fR .br Run \fIcc\fP in the background. .RE