Annotation of 43BSDReno/share/doc/usd/03.shell/t1, revision 1.1.1.1

1.1       root        1: .\"    @(#)t1  6.1 (Berkeley) 5/22/86
                      2: .\"
                      3: .EH 'USD:3-%''An Introduction to the UNIX Shell'
                      4: .OH 'An Introduction to the UNIX Shell''USD:3-%'
                      5: .\".RP
                      6: .TL 
                      7: An Introduction to the UNIX Shell
                      8: .AU
                      9: S. R. Bourne
                     10: .AI
                     11: .MH
                     12: .AU
                     13: (Updated for 4.3BSD by Mark Seiden)
                     14: .AB
                     15: .LP
                     16: The
                     17: .ul
                     18: shell\(dd
                     19: .FS 
                     20: \(dd This paper describes sh(1). If it's the c shell (csh) you're interested in, 
                     21: a good place to begin is William Joy's paper "An Introduction to the C shell" (USD:4).
                     22: .FE
                     23: is a command programming language that provides an interface
                     24: to the
                     25: .UX
                     26: operating system.
                     27: Its features include
                     28: control-flow primitives, parameter passing, variables and
                     29: string substitution.
                     30: Constructs such as
                     31: .ul
                     32: while, if then else, case
                     33: and
                     34: .ul
                     35: for
                     36: are available.
                     37: Two-way communication is possible between the
                     38: .ul
                     39: shell
                     40: and commands.
                     41: String-valued parameters, typically file names or flags, may be
                     42: passed to a command.
                     43: A return code is set by commands that may be used to determine control-flow,
                     44: and the standard output from a command may be used
                     45: as shell input.
                     46: .LP
                     47: The
                     48: .ul
                     49: shell
                     50: can modify the environment
                     51: in which commands run.
                     52: Input and output can be redirected
                     53: to files, and processes that communicate through `pipes'
                     54: can be invoked.
                     55: Commands are found by
                     56: searching directories
                     57: in the file system in a
                     58: sequence that can be defined by the user.
                     59: Commands can be read either from the terminal or from a file,
                     60: which allows command procedures to be
                     61: stored for later use.
                     62: .AE
                     63: .ds ST \v'.3m'\s+2*\s0\v'-.3m'
                     64: .SH
                     65: 1.0\ Introduction
                     66: .LP
                     67: The shell is both a command language
                     68: and a programming language
                     69: that provides an interface to the UNIX
                     70: operating system.
                     71: This memorandum describes, with
                     72: examples, the UNIX shell.
                     73: The first section covers most of the
                     74: everyday requirements
                     75: of terminal users.
                     76: Some familiarity with UNIX
                     77: is an advantage when reading this section;
                     78: see, for example,
                     79: "UNIX for beginners".
                     80: .[
                     81: unix beginn kernigh 1978
                     82: .]
                     83: Section 2 describes those features
                     84: of the shell primarily intended
                     85: for use within shell procedures.
                     86: These include the control-flow
                     87: primitives and string-valued variables
                     88: provided by the shell.
                     89: A knowledge of a programming language
                     90: would be a help when reading this section.
                     91: The last section describes the more
                     92: advanced features of the shell.
                     93: References of the form "see \fIpipe\fP (2)"
                     94: are to a section of the UNIX manual.
                     95: .[
                     96: seventh 1978 ritchie thompson
                     97: .]
                     98: .SH
                     99: 1.1\ Simple\ commands
                    100: .LP
                    101: Simple commands consist of one or more words
                    102: separated by blanks.
                    103: The first word is the name of the command
                    104: to be executed; any remaining words
                    105: are passed as arguments to the command.
                    106: For example,
                    107: .DS
                    108:        who
                    109: .DE
                    110: is a command that prints the names
                    111: of users logged in.
                    112: The command
                    113: .DS
                    114:        ls \(mil
                    115: .DE
                    116: prints a list of files in the current
                    117: directory.
                    118: The argument \fI\(mil\fP tells \fIls\fP
                    119: to print status information, size and
                    120: the creation date for each file.
                    121: .SH
                    122: 1.2\ Background\ commands
                    123: .LP
                    124: To execute a command the shell normally
                    125: creates a new \fIprocess\fP
                    126: and waits for it to finish.
                    127: A command may be run without waiting
                    128: for it to finish.
                    129: For example,
                    130: .DS
                    131:        cc pgm.c &
                    132: .DE
                    133: calls the C compiler to compile
                    134: the file \fIpgm.c\|.\fP
                    135: The trailing \fB&\fP is an operator that instructs the shell
                    136: not to wait for the command to finish.
                    137: To help keep track of such a process
                    138: the shell reports its process
                    139: number following its creation.
                    140: A list of currently active processes may be obtained
                    141: using the \fIps\fP command.
                    142: .SH
                    143: 1.3\ Input\ output\ redirection
                    144: .LP
                    145: Most commands produce output on the standard output
                    146: that is initially connected to the terminal.
                    147: This output may be sent to a file
                    148: by writing, for example,
                    149: .DS
                    150:        ls \(mil >file
                    151: .DE
                    152: The notation \fI>file\fP
                    153: is interpreted by the shell and is not passed
                    154: as an argument to \fIls.\fP
                    155: If \fIfile\fP does not exist then the
                    156: shell creates it;
                    157: otherwise the original contents of
                    158: \fIfile\fP are replaced with the output
                    159: from \fIls.\fP
                    160: Output may be appended to a file
                    161: using the notation
                    162: .DS
                    163:        ls \(mil \*(APfile
                    164: .DE
                    165: In this case \fIfile\fP is also created if it does not already
                    166: exist.
                    167: .LP
                    168: The standard input of a command may be taken
                    169: from a file instead of the terminal by
                    170: writing, for example,
                    171: .DS
                    172:        wc <file
                    173: .DE
                    174: The command \fIwc\fP reads its standard input
                    175: (in this case redirected from \fIfile\fP)
                    176: and prints the number of characters, words and
                    177: lines found.
                    178: If only the number of lines is required
                    179: then
                    180: .DS
                    181:        wc \(mil <file
                    182: .DE
                    183: could be used.
                    184: .SH
                    185: 1.4\ Pipelines\ and\ filters
                    186: .LP
                    187: The standard output of one command may be
                    188: connected to the standard input of another
                    189: by writing
                    190: the `pipe' operator,
                    191: indicated by \*(VT,
                    192: as in,
                    193: .DS
                    194:        ls \(mil \*(VT wc
                    195: .DE
                    196: Two commands connected in this way constitute
                    197: a \fIpipeline\fP and
                    198: the overall effect is the same as
                    199: .DS
                    200:        ls \(mil >file; wc <file
                    201: .DE
                    202: except that no \fIfile\fP is used.
                    203: Instead the two processes are connected
                    204: by a pipe (see \fIpipe\fP (2)) and are
                    205: run in parallel.
                    206: Pipes are unidirectional and
                    207: synchronization is achieved by
                    208: halting \fIwc\fP when there is
                    209: nothing to read and halting \fIls\fP
                    210: when the pipe is full.
                    211: .LP
                    212: A \fIfilter\fP is a command
                    213: that reads its standard input,
                    214: transforms it in some way,
                    215: and prints the result as output.
                    216: One such filter, \fIgrep,\fP
                    217: selects from its input those lines
                    218: that contain some specified string.
                    219: For example,
                    220: .DS
                    221:        ls \*(VT grep old
                    222: .DE
                    223: prints those lines, if any, of the output
                    224: from \fIls\fP that contain
                    225: the string \fIold.\fP
                    226: Another useful filter is \fIsort\fP.
                    227: For example,
                    228: .DS
                    229:        who \*(VT sort
                    230: .DE
                    231: will print an alphabetically sorted list
                    232: of logged in users.
                    233: .LP
                    234: A pipeline may consist of more than two commands,
                    235: for example,
                    236: .DS
                    237:        ls \*(VT grep old \*(VT wc \(mil
                    238: .DE
                    239: prints the number of file names
                    240: in the current directory containing
                    241: the string \fIold.\fP
                    242: .SH
                    243: 1.5\ File\ name\ generation
                    244: .LP
                    245: Many commands accept arguments
                    246: which are file names.
                    247: For example,
                    248: .DS
                    249:        ls \(mil main.c
                    250: .DE
                    251: prints information relating to the file \fImain.c\fP\|.
                    252: .LP
                    253: The shell provides a mechanism
                    254: for generating a list of file names
                    255: that match a pattern.
                    256: For example,
                    257: .DS
                    258:        ls \(mil \*(ST.c
                    259: .DE
                    260: generates, as arguments to \fIls,\fP
                    261: all file names in the current directory that end in \fI.c\|.\fP
                    262: The character \*(ST is a pattern that will match any string
                    263: including the null string.
                    264: In general \fIpatterns\fP are specified
                    265: as follows.
                    266: .RS
                    267: .IP \fB\*(ST\fR 8
                    268: Matches any string of characters
                    269: including the null string.
                    270: .IP \fB?\fR 8
                    271: Matches any single character.
                    272: .IP \fB[\*(ZZ]\fR 8
                    273: Matches any one of the characters
                    274: enclosed.
                    275: A pair of characters separated by a minus will
                    276: match any character lexically between
                    277: the pair.
                    278: .RE
                    279: .LP
                    280: For example,
                    281: .DS
                    282:        [a\(miz]\*(ST
                    283: .DE
                    284: matches all names in the current directory
                    285: beginning with
                    286: one of the letters \fIa\fP through \fIz.\fP
                    287: .DS
                    288:        /usr/fred/test/?
                    289: .DE
                    290: matches all names in the directory
                    291: \fB/usr/fred/test\fP that consist of a single character.
                    292: If no file name is found that matches
                    293: the pattern then the pattern is passed,
                    294: unchanged, as an argument.
                    295: .LP
                    296: This mechanism is useful both to save typing
                    297: and to select names according to some pattern.
                    298: It may also be used to find files.
                    299: For example,
                    300: .DS
                    301:        echo /usr/fred/\*(ST/core
                    302: .DE
                    303: finds and prints the names of all \fIcore\fP files in sub-directories
                    304: of \fB/usr/fred\|.\fP
                    305: (\fIecho\fP is a standard UNIX command that prints
                    306: its arguments, separated by blanks.)
                    307: This last feature can be expensive,
                    308: requiring a scan of all
                    309: sub-directories of \fB/usr/fred\|.\fP
                    310: .LP
                    311: There is one exception to the general
                    312: rules given for patterns.
                    313: The character `\fB.\fP'
                    314: at the start of a file name must be explicitly
                    315: matched.
                    316: .DS
                    317:        echo \*(ST
                    318: .DE
                    319: will therefore echo all file names in the current
                    320: directory not beginning
                    321: with `\fB.\fP'\|.
                    322: .DS
                    323:        echo \fB.\fP\*(ST
                    324: .DE
                    325: will echo all those file names that begin with `\fB.\fP'\|.
                    326: This avoids inadvertent matching
                    327: of the names `\fB.\fP' and `\fB..\fP'
                    328: which mean `the current directory'
                    329: and `the parent directory'
                    330: respectively.
                    331: (Notice that \fIls\fP suppresses
                    332: information for the files `\fB.\fP' and `\fB..\fP'\|.)
                    333: .SH
                    334: 1.6\ Quoting
                    335: .LP
                    336: Characters that have a special meaning
                    337: to the shell, such as \fB< > \*(ST ? \*(VT &\|,\fR
                    338: are called metacharacters.
                    339: A complete list of metacharacters is given
                    340: in appendix B.
                    341: Any character preceded by a \fB\\\fR is \fIquoted\fP
                    342: and loses its special meaning, if any.
                    343: The \fB\\\fP is elided so that
                    344: .DS
                    345:        echo \\\\?
                    346: .DE
                    347: will echo a single \fB?\|,\fP
                    348: and
                    349: .DS
                    350:        echo \\\\\\\\
                    351: .DE
                    352: will echo a single \fB\\\|.\fR
                    353: To allow long strings to be continued over
                    354: more than one line
                    355: the sequence \fB\\newline\fP
                    356: is ignored.
                    357: .LP
                    358: \fB\\\fP is convenient for quoting
                    359: single characters.
                    360: When more than one character needs
                    361: quoting the above mechanism is clumsy and
                    362: error prone.
                    363: A string of characters may be quoted
                    364: by enclosing the string between single quotes.
                    365: For example,
                    366: .DS
                    367:        echo xx\'\*(ST\*(ST\*(ST\*(ST\'xx
                    368: .DE
                    369: will echo
                    370: .DS
                    371:        xx\*(ST\*(ST\*(ST\*(STxx
                    372: .DE
                    373: The quoted string may not contain
                    374: a single quote
                    375: but may contain newlines, which are preserved.
                    376: This quoting mechanism is the most
                    377: simple and is recommended
                    378: for casual use.
                    379: .LP
                    380: A third quoting mechanism using double quotes
                    381: is also available
                    382: that prevents interpretation of some but not all
                    383: metacharacters.
                    384: Discussion of the
                    385: details is deferred
                    386: to section 3.4\|.
                    387: .SH
                    388: 1.7\ Prompting
                    389: .LP
                    390: When the shell is used from a terminal it will
                    391: issue a prompt before reading a command.
                    392: By default this prompt is `\fB$\ \fR'\|.
                    393: It may be changed by saying,
                    394: for example,
                    395: .DS
                    396:        \s-1PS1\s0=yesdear
                    397: .DE
                    398: that sets the prompt to be the string \fIyesdear\|.\fP
                    399: If a newline is typed and further input is needed
                    400: then the shell will issue the prompt `\fB>\ \fR'\|.
                    401: Sometimes this can be caused by mistyping
                    402: a quote mark.
                    403: If it is unexpected then an interrupt (\s-1DEL\s0)
                    404: will return the shell to read another command.
                    405: This prompt may be changed by saying, for example,
                    406: .DS
                    407:        \s-1PS2\s0=more
                    408: .DE
                    409: .SH
                    410: 1.8\ The\ shell\ and\ login
                    411: .LP
                    412: Following \fIlogin\fP (1)
                    413: the shell is called to read and execute
                    414: commands typed at the terminal.
                    415: If the user's login directory
                    416: contains the file \fB.profile\fP
                    417: then it is assumed to contain commands
                    418: and is read by the shell before reading
                    419: any commands from the terminal.
                    420: .SH
                    421: 1.9\ Summary
                    422: .sp
                    423: .RS
                    424: .IP \(bu
                    425: \fBls\fP
                    426: .br
                    427: Print the names of files in the current directory.
                    428: .IP \(bu
                    429: \fBls >file\fP
                    430: .br
                    431: Put the output from \fIls\fP into \fIfile.\fP
                    432: .IP \(bu
                    433: \fBls \*(VT wc \(mil\fR
                    434: .br
                    435: Print the number of files in the current directory.
                    436: .IP \(bu
                    437: \fBls \*(VT grep old\fR
                    438: .br
                    439: Print those file names containing the string \fIold.\fP
                    440: .IP \(bu
                    441: \fBls \*(VT grep old \*(VT wc \(mil\fR
                    442: .br
                    443: Print the number of files whose name contains the string \fIold.\fP
                    444: .IP \(bu
                    445: \fBcc pgm.c &\fR
                    446: .br
                    447: Run \fIcc\fP in the background.
                    448: .RE

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.