rc

[fork] interactive rc shell
git clone https://hhvn.uk/rc
git clone git://hhvn.uk/rc
Log | Files | Refs | README | LICENSE

rc.1 (48656B)


      1 .\" rc.1
      2 .\"-------
      3 .\" Man page portability notes
      4 .\"
      5 .\" These are some notes on conventions to maintain for greatest
      6 .\" portability of this man page to various other versions of
      7 .\" nroff.
      8 .\"
      9 .\" When you want a \ to appear in the output, use \e in the man page.
     10 .\" (NOTE this comes up in the rc grammar, where to print out '\n' the
     11 .\" man page must contain '\en'.)
     12 .\"
     13 .\" Evidently not all versions of nroff allow the omission of the
     14 .\" terminal " on a macro argument. Thus what could be written
     15 .\"
     16 .\" .Cr "exec >[2] err.out
     17 .\"
     18 .\" in true nroffs must be written
     19 .\"
     20 .\" .Cr "exec >[2] err.out"
     21 .\"
     22 .\" instead.
     23 .\"
     24 .\" Use symbolic font names (e.g. R, I, B) instead of the standard
     25 .\" font positions 1, 2, 3. Note that for Xf to work the standard
     26 .\" font names must be single characters.
     27 .\"
     28 .\" Not all man macros have the RS and RE requests (I altered the Ds
     29 .\" and De macros and the calls to Ds accordingly).
     30 .\"
     31 .\" Thanks to Michael Haardt (u31b3hs@cip-s01.informatik.rwth-aachen.de)
     32 .\" for pointing out these problems.
     33 .\"
     34 .\" Note that sentences should end at the end of a line. nroff and
     35 .\" troff will supply the correct inter-sentence spacing, but only if
     36 .\" the sentences end at the end of a line. Explicit spaces, if given,
     37 .\" are apparently honored and the normal inter-sentence spacing is
     38 .\" suppressed.
     39 .\"
     40 .\" DaviD W. Sanderson
     41 .\"-------
     42 .\" Dd	distance to space vertically before a "display"
     43 .\" These are what n/troff use for inter-paragraph distance
     44 .\"-------
     45 .if t .nr Dd .4v
     46 .if n .nr Dd 1v
     47 .\"-------
     48 .\" Ds	begin a display, indented .5 inches from the surrounding text.
     49 .\"
     50 .\" Note that uses of Ds and De may NOT be nested.
     51 .\"-------
     52 .de Ds
     53 .\" .RS \\$1
     54 .sp \\n(Ddu
     55 .in +0.5i
     56 .nf
     57 ..
     58 .\"-------
     59 .\" De	end a display (no trailing vertical spacing)
     60 .\"-------
     61 .de De
     62 .fi
     63 .in
     64 .\" .RE
     65 ..
     66 .\"-------
     67 .\" I stole the Xf macro from the -man macros on my machine (originally
     68 .\" "}S", I renamed it so that it won't conflict).
     69 .\"-------
     70 .\" Set Cf to the name of the constant width font.
     71 .\" It will be "C" or "(CW", typically.
     72 .\" NOTEZ BIEN the lines defining Cf must have no trailing white space:
     73 .\"-------
     74 .if t .ds Cf C
     75 .if n .ds Cf R
     76 .\"-------
     77 .\" Rc - Alternate Roman and Courier
     78 .\"-------
     79 .de Rc
     80 .Xf R \\*(Cf \& "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6"
     81 ..
     82 .\"-------
     83 .\" Ic - Alternate Italic and Courier
     84 .\"-------
     85 .de Ic
     86 .Xf I \\*(Cf \& "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6"
     87 ..
     88 .\"-------
     89 .\" Bc - Alternate Bold and Courier
     90 .\"-------
     91 .de Bc
     92 .Xf B \\*(Cf \& "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6"
     93 ..
     94 .\"-------
     95 .\" Cr - Alternate Courier and Roman
     96 .\"-------
     97 .de Cr
     98 .Xf \\*(Cf R \& "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6"
     99 ..
    100 .\"-------
    101 .\" Ci - Alternate Courier and Italic
    102 .\"-------
    103 .de Ci
    104 .Xf \\*(Cf I \& "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6"
    105 ..
    106 .\"-------
    107 .\" Cb - Alternate Courier and Bold
    108 .\"-------
    109 .de Cb
    110 .Xf \\*(Cf B \& "\\$1" "\\$2" "\\$3" "\\$4" "\\$5" "\\$6"
    111 ..
    112 .\"-------
    113 .\" Xf - Alternate fonts
    114 .\"
    115 .\" \$1 - first font
    116 .\" \$2 - second font
    117 .\" \$3 - desired word with embedded font changes, built up by recursion
    118 .\" \$4 - text for first font
    119 .\" \$5 - \$9 - remaining args
    120 .\"
    121 .\" Every time we are called:
    122 .\"
    123 .\" If		there is something in \$4
    124 .\" then	Call ourself with the fonts switched,
    125 .\"		with a new word made of the current word (\$3) and \$4
    126 .\"		rendered in the first font,
    127 .\"		and with the remaining args following \$4.
    128 .\" else	We are done recursing. \$3 holds the desired output
    129 .\"		word. We emit \$3, change to Roman font, and restore
    130 .\"		the point size to the default.
    131 .\" fi
    132 .\"
    133 .\" Use Xi to add a little bit of space after italic text.
    134 .\"-------
    135 .de Xf
    136 .ds Xi
    137 .\"-------
    138 .\" I used to test for the italic font both by its font position
    139 .\" and its name. Now just test by its name.
    140 .\"
    141 .\" .if "\\$1"2" .if !"\\$5"" .ds Xi \^
    142 .\"-------
    143 .if "\\$1"I" .if !"\\$5"" .ds Xi \^
    144 .\"-------
    145 .\" This is my original code to deal with the recursion.
    146 .\" Evidently some nroffs can't deal with it.
    147 .\"-------
    148 .\" .ie !"\\$4"" \{\
    149 .\" .	Xf \\$2 \\$1 "\\$3\\f\\$1\\$4\\*(Xi" "\\$5" "\\$6" "\\$7" "\\$8" "\\$9"
    150 .\" .\}
    151 .\" .el \{\\$3
    152 .\" .	ft R	\" Restore the default font, since we don't know
    153 .\" .		\" what the last font change was.
    154 .\" .	ps 10	\" Restore the default point size, since it might
    155 .\" .		\" have been changed by an argument to this macro.
    156 .\" .\}
    157 .\"-------
    158 .\" Here is more portable (though less pretty) code to deal with
    159 .\" the recursion.
    160 .\"-------
    161 .if !"\\$4"" .Xf \\$2 \\$1 "\\$3\\f\\$1\\$4\\*(Xi" "\\$5" "\\$6" "\\$7" "\\$8" "\\$9"
    162 .if "\\$4"" \\$3\fR\s10
    163 ..
    164 .TH RC 1 "2015-05-13"
    165 .SH NAME
    166 rc \- shell
    167 .SH SYNOPSIS
    168 .B rc
    169 .RB [ \-deiIlnopsvx ]
    170 .RB [ \-c
    171 .IR command ]
    172 .RI [ arguments ]
    173 .SH DESCRIPTION
    174 .I rc
    175 is a command interpreter and programming language similar to
    176 .IR sh (1).
    177 It is based on the AT&T Plan 9 shell of the same name.
    178 The shell offers a C-like syntax (much more so than the C shell),
    179 and a powerful mechanism for manipulating variables.
    180 It is reasonably small and reasonably fast,
    181 especially when compared to contemporary shells.
    182 Its use is intended to be interactive,
    183 but the language lends itself well to scripts.
    184 .SH OPTIONS
    185 .TP
    186 .Cr \-c
    187 If
    188 .Cr \-c
    189 is present, commands are executed from the immediately following
    190 argument.
    191 Any further arguments to
    192 .I rc
    193 are placed in
    194 .Cr $* .
    195 Thus:
    196 .Ds
    197 .Cr "rc -c 'echo $*' 1 2 3"
    198 .De
    199 .TP
    200 \&
    201 prints out
    202 .Ds
    203 .Cr "1 2 3"
    204 .De
    205 .TP
    206 .Cr \-d
    207 This flag causes
    208 .I rc
    209 not to ignore
    210 .Cr SIGQUIT
    211 or
    212 .Cr SIGTERM .
    213 Thus
    214 .I rc
    215 can be made to dump core if sent
    216 .Cr SIGQUIT .
    217 This flag is only useful for debugging
    218 .IR rc .
    219 .TP
    220 .Cr \-e
    221 If the
    222 .Cr \-e
    223 flag is present, then
    224 .I rc
    225 will exit if any command fails (exits with non-zero status). However
    226 .I "rc -e"
    227 does not exit if a conditional fails. A conditional is the test of an
    228 .Cr "if ()"
    229 command, the test of a
    230 .Cr "while ()"
    231 command, or the left hand side of the
    232 .Cr ||
    233 or the
    234 .Cr &&
    235 operator.
    236 .TP
    237 .Cr \-i
    238 If the
    239 .Cr \-i
    240 flag is present or if the input to
    241 .I rc
    242 is from a terminal (as determined by
    243 .IR isatty (3))
    244 then
    245 .I rc
    246 will be in
    247 .I interactive
    248 mode.
    249 That is, a prompt (from
    250 .Cr $prompt(1)\^ )
    251 will be printed before an input line is taken, and
    252 .I rc
    253 will ignore
    254 .Cr SIGINT .
    255 .TP
    256 .Cr \-I
    257 If the
    258 .Cr \-I
    259 flag is present, or if the input to
    260 .I rc
    261 is not from a terminal, then
    262 .I rc
    263 will not be in interactive mode.
    264 No prompts will be printed, and
    265 .Cr SIGINT
    266 will cause
    267 .I rc
    268 to exit.
    269 .TP
    270 .Cr \-l
    271 If the
    272 .Cr \-l
    273 flag is present, or if
    274 .IR rc 's
    275 .Cr argv[0][0]
    276 is a dash
    277 .Rc ( \- ),
    278 then
    279 .I rc
    280 will behave as a login shell.
    281 That is, it will run commands from
    282 .Cr $home/.rcrc ,
    283 if this file exists, before reading any other input.
    284 .TP
    285 .Cr \-n
    286 This flag causes
    287 .I rc
    288 to read its input and parse it, but not to execute any commands.
    289 This is useful for syntax checking on scripts.
    290 If used in combination with the
    291 .Cr \-x
    292 flag,
    293 .I rc
    294 will print each command as it is parsed in a form similar to the one
    295 used for exporting functions into the environment.
    296 .TP
    297 .Cr \-o
    298 This flag prevents the usual practice of trying to open
    299 .Cr /dev/null
    300 on file descriptors 0, 1, and 2, if any of those descriptors
    301 are inherited closed.
    302 .TP
    303 .Cr \-p
    304 This flag prevents
    305 .I rc
    306 from initializing shell functions from the environment.
    307 This allows
    308 .I rc
    309 to run in a protected mode, whereby it becomes more difficult for
    310 an
    311 .I rc
    312 script to be subverted by placing false commands in the environment.
    313 (Note that the presence of this flag does
    314 .I not
    315 mean that it is safe to run setuid
    316 .I rc
    317 scripts; the usual caveats about the setuid bit still apply.)
    318 .TP
    319 .Cr \-s
    320 This flag causes
    321 .I rc
    322 to read from standard input.
    323 Any arguments are placed in
    324 .Cr $* .
    325 .TP
    326 .Cr \-v
    327 This flag causes
    328 .I rc
    329 to echo its input
    330 to standard error as it is read.
    331 .TP
    332 .Cr \-x
    333 This flag causes
    334 .I rc
    335 to print every command on standard error before it is executed.
    336 It can be useful for debugging
    337 .I rc
    338 scripts.
    339 .PP
    340 .SH COMMANDS
    341 A simple command is a sequence of words, separated by white space
    342 (space and tab) characters that ends with a newline, semicolon
    343 .Rc ( ; ),
    344 or ampersand
    345 .Rc ( & ).
    346 The first word of a command is the name of that command.
    347 If the name begins with
    348 .Cr / ,
    349 .Cr ./ ,
    350 or
    351 .Cr ../ ,
    352 then the name is used as an absolute path
    353 name referring to an executable file.
    354 Otherwise, the name of the command is looked up in a table
    355 of shell functions, builtin commands,
    356 or as a file in the directories named by
    357 .Cr $path .
    358 .SS "Background Tasks"
    359 A command ending with
    360 .Cr &
    361 is run in the background; that is,
    362 the shell returns immediately rather than waiting for the command to
    363 complete.
    364 Background commands have
    365 .Cr /dev/null
    366 connected to their standard input unless an explicit redirection for
    367 standard input is used.
    368 .SS "Subshells"
    369 A command prefixed with an at-sign
    370 .Rc ( @ )
    371 is executed in a subshell.
    372 This insulates the parent shell from the effects
    373 of state changing operations such as a
    374 .B cd
    375 or a variable assignment.
    376 For example:
    377 .Ds
    378 .Cr "@ {cd ..; make}"
    379 .De
    380 .PP
    381 will run
    382 .IR make (1)
    383 in the parent directory
    384 .Rc ( .. ),
    385 but leaves the shell running in the current directory.
    386 .SS "Line continuation"
    387 A long logical line may be continued over several physical lines by
    388 terminating each line (except the last) with a backslash
    389 .Rc ( \e ).
    390 The backslash-newline sequence is treated as a space.
    391 A backslash is not otherwise special to
    392 .IR rc .
    393 (In addition,
    394 inside quotes a backslash loses its special meaning
    395 even when it is followed by a newline.)
    396 .SS Quoting
    397 .IR rc
    398 interprets several characters specially; special characters
    399 automatically terminate words.
    400 The following characters are special:
    401 .Ds
    402 .Cr "# ; & | ^ $ \` ' { } ( ) < >"
    403 .De
    404 .PP
    405 The single quote
    406 .Rc ( ' )
    407 prevents special treatment of any character other than itself.
    408 All characters, including control characters, newlines,
    409 and backslashes between two quote characters are treated as an
    410 uninterpreted string.
    411 A quote character itself may be quoted by placing two quotes in a row.
    412 The minimal sequence needed to enter the quote character is
    413 .Cr '''' .
    414 The empty string is represented by
    415 .Cr '' .
    416 Thus:
    417 .Ds
    418 .Cr "echo 'What''s the plan, Stan?'"
    419 .De
    420 .PP
    421 prints out
    422 .Ds
    423 .Cr "What's the plan, Stan?"
    424 .De
    425 .PP
    426 The number sign
    427 .Rc ( # )
    428 begins a comment in
    429 .IR rc .
    430 All characters up to but not including the next newline are ignored.
    431 Note that backslash continuation does not work inside a comment,
    432 i.e.,
    433 the backslash is ignored along with everything else.
    434 .SS Grouping
    435 Zero or more commands may be grouped within braces
    436 .Rc (`` { ''
    437 and
    438 .Rc `` } ''),
    439 and are then treated as one command.
    440 Braces do not otherwise define scope;
    441 they are used only for command grouping.
    442 In particular, be wary of the command:
    443 .Ds
    444 .Cr "for (i) {"
    445 .Cr "    command"
    446 .Cr "} | command"
    447 .De
    448 .PP
    449 Since pipe binds tighter than
    450 .Cr for ,
    451 this command does not perform what the user expects it to.
    452 Instead, enclose the whole
    453 .Cr for
    454 statement in braces:
    455 .Ds
    456 .Cr "{for (i) command} | command"
    457 .De
    458 .PP
    459 Fortunately,
    460 .IR rc 's
    461 grammar is simple enough that a (confident) user can
    462 understand it by examining the skeletal
    463 .IR yacc (1)
    464 grammar
    465 at the end of this man page (see the section entitled
    466 .BR GRAMMAR ).
    467 .SS "Input and output"
    468 .PP
    469 The standard output may be redirected to a file with
    470 .Ds
    471 .Cr "command > file"
    472 .De
    473 .PP
    474 and the standard input may be taken from a file with
    475 .Ds
    476 .Cr "command < file"
    477 .De
    478 .PP
    479 Redirections can appear anywhere in the line: the word
    480 following the redirection symbol is the filename and must be
    481 quoted if it contains spaces or other special characters.
    482 These are all equivalent.
    483 .Ds
    484 .Cr "echo 1 2 3 > foo"
    485 .Cr "> foo echo 1 2 3"
    486 .Cr "echo 1 2 > foo 3"
    487 .De
    488 .PP
    489 File descriptors other than 0 and 1 may be specified also.
    490 For example, to redirect standard error to a file, use:
    491 .Ds
    492 .Cr "command >[2] file"
    493 .De
    494 .PP
    495 In order to duplicate a file descriptor, use
    496 .Ci >[ n = m ]\fR.
    497 Thus to redirect both standard output and standard error
    498 to the same file, use
    499 .Ds
    500 .Cr "command > file >[2=1]"
    501 .De
    502 .PP
    503 As in
    504 .IR sh ,
    505 redirections are processed from left to right.
    506 Thus this sequence
    507 .Ds
    508 .Cr "command >[2=1] > file"
    509 .De
    510 .PP
    511 is usually a mistake.
    512 It first duplicates standard error to standard
    513 output; then redirects standard output to a file, leaving standard error
    514 wherever standard output originally was.
    515 .PP
    516 To close a file descriptor that may be open, use
    517 .Ci >[ n =]\fR.
    518 For example, to
    519 close file descriptor 7:
    520 .Ds
    521 .Cr "command >[7=]"
    522 .De
    523 .PP
    524 Note that no spaces may appear in these constructs:
    525 .Ds
    526 .Cr "command > [2] file"
    527 .De
    528 .PP
    529 would send the output of the command to a file named
    530 .Cr [2] ,
    531 with the intended filename appearing in the command's argument list.
    532 .PP
    533 In order to place the output of a command at the end of an already
    534 existing file, use:
    535 .Ds
    536 .Cr "command >> file"
    537 .De
    538 .PP
    539 If the file does not exist, then it is created.
    540 .PP
    541 ``Here documents'' are supported as in
    542 .I sh
    543 with the use of
    544 .Ds
    545 .Cr "command << 'eof-marker'"
    546 .De
    547 .PP
    548 Subsequent lines form the standard input of
    549 the command, till a line containing just the
    550 marker, in this case
    551 .Cr eof-marker ,
    552 is encountered.
    553 .PP
    554 If the end-of-file marker is enclosed in quotes,
    555 then no variable substitution occurs inside the here document.
    556 Otherwise, every variable is substituted
    557 by its space-separated-list value (see
    558 .BR "Flat Lists" ,
    559 below),
    560 and if a
    561 .Cr ^
    562 character follows a variable name, it is deleted.
    563 This allows the unambiguous use of variables adjacent to text, as in
    564 .Ds
    565 .Cr $variable^follow
    566 .De
    567 .PP
    568 To include a literal
    569 .Cr $
    570 in a here document when an unquoted end-of-file marker is being used,
    571 enter it as
    572 .Cr $$ .
    573 .PP
    574 Additionally,
    575 .I rc
    576 supports ``here strings'', which are like here documents,
    577 except that input is taken directly from a string on the command line.
    578 Their use is illustrated here:
    579 .Ds
    580 .Cr "cat <<< 'this is a here string' | wc"
    581 .De
    582 .PP
    583 (This feature enables
    584 .I rc
    585 to export functions using here documents into the environment;
    586 the author does not expect users to find this feature useful.)
    587 .SS Pipes
    588 Two or more commands may be combined in a pipeline by placing the
    589 vertical bar
    590 .Rc ( \||\| )
    591 between them.
    592 The standard output (file descriptor 1)
    593 of the command on the left is tied to the standard input (file
    594 descriptor 0) of the command on the right.
    595 The notation
    596 .Ci |[ n = m ]
    597 indicates that file descriptor
    598 .I n
    599 of the left process is connected to
    600 file descriptor
    601 .I m
    602 of the right process.
    603 .Ci |[ n ]
    604 is a shorthand for
    605 .Ci |[ n =0]\fR.
    606 As an example, to pipe the standard error of a command to
    607 .IR wc (1),
    608 use:
    609 .Ds
    610 .Cr "command |[2] wc"
    611 .De
    612 .PP
    613 As with file redirections, no spaces may occur in the construct specifying
    614 numbered file descriptors.
    615 .PP
    616 The exit status of a pipeline is considered true if and only if every
    617 command in the pipeline exits true.
    618 .SS "Commands as Arguments"
    619 Some commands, like
    620 .IR cmp (1)
    621 or
    622 .IR diff (1),
    623 take their arguments on the command
    624 line, and do not read input from standard input.
    625 It is convenient
    626 sometimes to build nonlinear pipelines so that a command like
    627 .I cmp
    628 can read the output of two other commands at once.
    629 .I rc
    630 does it like this:
    631 .Ds
    632 .Cr "cmp <{command} <{command}"
    633 .De
    634 .PP
    635 compares the output of the two commands in braces.
    636 Note: since this form of
    637 redirection is implemented with some kind of pipe, and since one cannot
    638 .IR lseek (2)
    639 on a pipe, commands that use
    640 .IR lseek (2)
    641 will hang.
    642 For example, some versions of
    643 .IR diff (1)
    644 use
    645 .IR lseek (2)
    646 on their inputs.
    647 .PP
    648 Data can be sent down a pipe to several commands using
    649 .IR tee (1)
    650 and the output version of this notation:
    651 .Ds
    652 .Cr "echo hi there | tee >{sed 's/^/p1 /'} >{sed 's/^/p2 /'}"
    653 .De
    654 .SH "CONTROL STRUCTURES"
    655 The following may be used for control flow in
    656 .IR rc :
    657 .SS "If-Else Statements"
    658 .PD 0
    659 .sp
    660 .Ci "if (" test ") {"
    661 .br
    662 .I "    cmd"
    663 .br
    664 .TP
    665 .Ci "} else " cmd
    666 The
    667 .I test
    668 is executed, and if its return status is zero, the first
    669 command is executed, otherwise the second is.
    670 Braces are not mandatory around the commands.
    671 However, an
    672 .Cr else
    673 statement is valid only if it
    674 follows a close-brace on the same line.
    675 Otherwise, the
    676 .Cr if
    677 is taken to be a simple-if:
    678 .Ds
    679 .Cr "if (test)"
    680 .Cr "    command"
    681 .De
    682 .PD
    683 .SS "While and For Loops"
    684 .TP
    685 .Ci "while (" test ) " cmd"
    686 .I rc
    687 executes the
    688 .I test
    689 and performs the command as long as the
    690 .I test
    691 is true.
    692 .TP
    693 .Ci "for (" var " in " list ) " cmd"
    694 .I rc
    695 sets
    696 .I var
    697 to each element of
    698 .I list
    699 (which may contain variables and backquote substitutions) and runs
    700 .IR cmd .
    701 If
    702 .Rc `` in
    703 .IR list ''
    704 is omitted, then
    705 .I rc
    706 will set
    707 .I var
    708 to each element of
    709 .Cr $* .
    710 For example:
    711 .Ds
    712 .Cr "for (i in \`{ls -F | grep '\e*$' | sed 's/\e*$//'}) { commands }"
    713 .De
    714 .TP
    715 \&
    716 will set
    717 .Cr $i
    718 to the name of each file in the current directory that is
    719 executable.
    720 .SS "Switch"
    721 .TP
    722 .Ci "switch (" list ") { case" " ..." " }"
    723 .I rc
    724 looks inside the braces after a
    725 .Cr switch
    726 for statements beginning with the word
    727 .Cr case .
    728 If any of the patterns following
    729 .Cr case
    730 match the list supplied to
    731 .Cr switch ,
    732 then the commands up until the next
    733 .Cr case
    734 statement are executed.
    735 The metacharacters
    736 .Cr "*" ,
    737 .Cr [
    738 or
    739 .Cr ?
    740 should not be quoted;
    741 matching is performed only against the strings in
    742 .IR list ,
    743 not against file names.
    744 (Matching for case statements is the same as for the
    745 .Cr ~
    746 command.)
    747 .SS "Logical Operators"
    748 There are a number of operators in
    749 .I rc
    750 which depend on the exit status of a command.
    751 .Ds
    752 .Cr "command && command"
    753 .De
    754 .PP
    755 executes the first command and then executes the second command if and only if
    756 the first command exits with a zero exit status (``true'' in Unix).
    757 .Ds
    758 .Cr "command || command"
    759 .De
    760 .PP
    761 executes the first command and then executes the second command if and only if
    762 the first command exits with a nonzero exit status (``false'' in Unix).
    763 .Ds
    764 .Cr "! command"
    765 .De
    766 .PP
    767 negates the exit status of a command.
    768 .SH "PATTERN MATCHING"
    769 There are two forms of pattern matching in
    770 .IR rc .
    771 One is traditional shell globbing.
    772 This occurs in matching for file names in argument lists:
    773 .Ds
    774 .Cr "command argument argument ..."
    775 .De
    776 .PP
    777 When the characters
    778 .Cr "*" ,
    779 .Cr [
    780 or
    781 .Cr ?
    782 occur in an argument or command,
    783 .I rc
    784 looks at the
    785 argument as a pattern for matching against files.
    786 (Contrary to the behavior other shells exhibit,
    787 .I rc
    788 will only perform pattern matching if a metacharacter occurs unquoted and
    789 literally in the input.
    790 Thus,
    791 .Ds
    792 .Cr "foo='*'"
    793 .Cr "echo $foo"
    794 .De
    795 .PP
    796 will always echo just a star.
    797 In order for non-literal metacharacters to be expanded, an
    798 .Cr eval
    799 statement must be used in order to rescan the input.)
    800 Pattern matching occurs according to the following rules: a
    801 .Cr *
    802 matches any number (including zero) of
    803 characters.
    804 A
    805 .Cr ?
    806 matches any single character, and a
    807 .Cr [
    808 followed by a
    809 number of characters followed by a
    810 .Cr ]
    811 matches a single character in that
    812 class.
    813 The rules for character class matching are the same as those for
    814 .IR ed (1),
    815 with the exception that character class negation is achieved
    816 with the tilde
    817 .Rc ( ~ ),
    818 not the caret
    819 .Rc ( ^ ),
    820 since the caret already means
    821 something else in
    822 .IR rc .
    823 .PP
    824 .I rc
    825 also matches patterns against strings with the
    826 .Cr ~
    827 command:
    828 .Ds
    829 .Cr "~ subject pattern pattern ..."
    830 .De
    831 .PP
    832 The
    833 .Cr ~
    834 command succeeds (sets
    835 .Cr $status
    836 to zero) if and only if one of the
    837 .Cr pattern s
    838 matches
    839 .Cr subject .
    840 Thus
    841 .Ds
    842 .Cr "~ foo f*"
    843 .De
    844 .PP
    845 succeeds (sets status to zero), while
    846 .Ds
    847 .Cr "~ bar f*"
    848 .De
    849 .PP
    850 fails (sets status to one).
    851 .PP
    852 The null list is matched by the null list, so
    853 .Ds
    854 .Cr "~ $foo ()"
    855 .De
    856 .PP
    857 checks to see whether
    858 .Cr $foo
    859 is empty or not. Because
    860 .I rc
    861 does not have hierarchical lists, the test for emptiness cannot be
    862 combined with other tests. To test whether
    863 .Cr $foo
    864 is empty, or one of the strings
    865 .Cr nada
    866 or
    867 .Cr rien ,
    868 do
    869 .I not
    870 write
    871 .Ds
    872 .Cr "~ $x () nada rien # WRONG means the same as: ~ $x nada rien"
    873 .De
    874 .PP
    875 instead write
    876 .Ds
    877 .Cr "~ $x () || ~ $x nada rien"
    878 .De
    879 .PP
    880 Another way to test if
    881 .Cr $foo
    882 is empty is
    883 .Ds
    884 .Cr "~ $#foo 0"
    885 .De
    886 .PP
    887 Note that inside a
    888 .Cr ~
    889 command
    890 .I rc
    891 does not match patterns against file
    892 names, so it is not necessary to quote the characters
    893 .Cr "*" ,
    894 .Cr [
    895 and
    896 .Cr "?" .
    897 However,
    898 .I rc
    899 does expand the subject against filenames if it contains
    900 metacharacters.
    901 Thus, the command
    902 .Ds
    903 .Cr "~ * ?"
    904 .De
    905 .PP
    906 succeeds if any of the files in the current directory have a
    907 single-character name.
    908 .PP
    909 If the
    910 .Cr ~
    911 command is given a list as its first
    912 argument, then a successful match against any of the elements of that
    913 list will cause
    914 .Cr ~
    915 to succeed.
    916 For example:
    917 .Ds
    918 .Cr "~ (foo goo zoo) z*"
    919 .De
    920 .PP
    921 is true.
    922 .SH "LISTS AND VARIABLES"
    923 The primary data structure in
    924 .IR rc
    925 is the list, which is a sequence of words.
    926 Parentheses are used to group lists.
    927 The empty list is represented by
    928 .Cr "()" .
    929 Lists have no hierarchical structure;
    930 a list inside another list is expanded so the
    931 outer list contains all the elements of the inner list.
    932 Thus, the following are all equivalent
    933 .Ds
    934 .Cr "one two three"
    935 
    936 .Cr "(one two three)"
    937 
    938 .Cr "((one) () ((two three)))"
    939 .De
    940 .PP
    941 Note that the null string,
    942 .Cr "''" ,
    943 and the null list,
    944 .Cr "()" ,
    945 are two very different things.
    946 Assigning the null string to a variable is a valid operation, but it
    947 does not remove its definition.
    948 .Ds
    949 .Cr "null = '' empty = () echo $#null $#empty"
    950 .De
    951 .PP
    952 produces the output
    953 .Ds
    954 .Cr "1 0"
    955 .De
    956 .SS "List Concatenation"
    957 Two lists may be joined by the concatenation operator
    958 .Rc ( ^ ).
    959 Concatenation works according to the following rules:
    960 if the two lists have the same number of elements,
    961 then concatenation is pairwise:
    962 .Ds
    963 .Cr "echo (a\- b\- c\-)^(1 2 3)"
    964 .De
    965 .PP
    966 produces the output
    967 .Ds
    968 .Cr "a\-1 b\-2 c\-3"
    969 .De
    970 .PP
    971 Otherwise, at least one of the lists must have a single element,
    972 and then the concatenation is distributive:
    973 .Ds
    974 .Cr "cc \-^(O g c) (malloc alloca)^.c"
    975 .De
    976 .PP
    977 has the effect of performing the command
    978 .Ds
    979 .Cr "cc \-O \-g \-c malloc.c alloca.c"
    980 .De
    981 .PP
    982 A single word is a list of length one, so
    983 .Ds
    984 .Cr "echo foo^bar"
    985 .De
    986 .PP
    987 produces the output
    988 .Ds
    989 .Cr foobar
    990 .De
    991 .SS "Free Carets"
    992 .I rc
    993 inserts carets (concatenation operators) for free in certain situations,
    994 in order to save some typing on the user's behalf.
    995 For
    996 example, the above example could also be typed in as:
    997 .Ds
    998 .Cr "opts=(O g c) files=(malloc alloca) cc \-$opts $files.c"
    999 .De
   1000 .PP
   1001 .I rc
   1002 takes care to insert a free caret between the
   1003 .Rc `` \- ''
   1004 and
   1005 .Cr "$opts" ,
   1006 as well
   1007 as between
   1008 .Cr $files
   1009 and
   1010 .Cr ".c" .
   1011 The rule for free carets is as follows: if
   1012 a word or keyword is immediately
   1013 followed by another word, keyword, dollar-sign or
   1014 backquote, then
   1015 .I rc
   1016 inserts a caret between them.
   1017 .SS "Variables"
   1018 A list may be assigned to a variable, using the notation:
   1019 .Ds
   1020 .Ic var " = " list
   1021 .De
   1022 .PP
   1023 The special variable
   1024 .Cr *
   1025 may also be assigned to using this notation;
   1026 .I rc
   1027 has no
   1028 .B set
   1029 builtin.
   1030 .PP
   1031 Any non-empty sequence of characters, except a sequence including only
   1032 digits, may be used as a variable name.
   1033 Any character except
   1034 .Cr =
   1035 may be used, but special characters must be quoted.
   1036 All user-defined variables are exported into the environment.
   1037 .PP
   1038 The value of a variable is referenced with the dollar
   1039 .Rc ( $ )
   1040 operator:
   1041 .Ds
   1042 .Ci $ var
   1043 .De
   1044 .PP
   1045 Any variable which has not been assigned a value returns the null list,
   1046 .Cr "()" ,
   1047 when referenced.
   1048 Multiple references are allowed:
   1049 .Ds
   1050 .Cr "a = foo"
   1051 .Cr "b = a"
   1052 .Cr "echo $ $ b"
   1053 .De
   1054 .PP
   1055 prints
   1056 .Ds
   1057 .Cr foo
   1058 .De
   1059 .PP
   1060 A variable's definition may also be removed by
   1061 assigning the null list to a variable:
   1062 .Ds
   1063 .Ic var =()
   1064 .De
   1065 .PP
   1066 For ``free careting'' to work correctly,
   1067 .I rc
   1068 must make certain assumptions
   1069 about what characters may appear in a variable name.
   1070 .I rc
   1071 assumes that a variable name consists only of alphanumeric characters,
   1072 underscore
   1073 .Rc ( \|_\| )
   1074 and star
   1075 .Rc ( * ).
   1076 To reference a variable with other
   1077 characters in its name, quote the variable name.
   1078 Thus:
   1079 .Ds
   1080 .Cr "echo $'we$Ird:Variab!le'"
   1081 .De
   1082 .SS "Local Variables"
   1083 Any number of variable assignments may be made local to a single
   1084 command by typing:
   1085 .Ds
   1086 .Cr "a=foo b=bar ... command"
   1087 .De
   1088 .PP
   1089 The command may be a compound command, so for example:
   1090 .Ds
   1091 .Cr "path=. ifs=() {"
   1092 .Cr "    " ...
   1093 .Cr }
   1094 .De
   1095 .PP
   1096 sets
   1097 .Cr path
   1098 to
   1099 .Cr .
   1100 and removes
   1101 .Cr ifs
   1102 for the duration of one long compound command.
   1103 .SS "Variable Subscripts"
   1104 Variables may be subscripted with the notation
   1105 .Ds
   1106 .Ci $var( n )
   1107 .De
   1108 .PP
   1109 where
   1110 .I n
   1111 is a list of integers (origin 1).
   1112 The opening parenthesis must immediately follow the variable name.
   1113 The list of subscripts need not be in order or even unique.
   1114 Thus,
   1115 .Ds
   1116 .Cr "a=(one two three)"
   1117 .Cr "echo $a(3 3 3)"
   1118 .De
   1119 .PP
   1120 prints
   1121 .Ds
   1122 .Cr "three three three"
   1123 .De
   1124 .PP
   1125 If
   1126 .I n
   1127 references a nonexistent element, then
   1128 .Ci $var( n )
   1129 returns the null list.
   1130 The notation
   1131 .Ci "$" n\fR,
   1132 where
   1133 .I n
   1134 is an integer, is a shorthand for
   1135 .Ci $*( n )\fR.
   1136 Thus,
   1137 .IR rc 's
   1138 arguments may be referred to as
   1139 .Cr "$1" ,
   1140 .Cr "$2" ,
   1141 and so on.
   1142 .PP
   1143 Note also that the list of subscripts may be given by any of
   1144 .IR rc 's
   1145 list operations:
   1146 .Ds
   1147 .Cr "$var(\`{awk 'BEGIN{for(i=1;i<=10;i++)print i;exit; }'})"
   1148 .De
   1149 .PP
   1150 returns the first 10 elements of
   1151 .Cr $var .
   1152 .PP
   1153 To count the number of elements in a variable, use
   1154 .Ds
   1155 .Cr $#var
   1156 .De
   1157 .PP
   1158 This returns a single-element list, with the number of elements in
   1159 .Cr $var .
   1160 .SS "Flat Lists"
   1161 In order to create a single-element list from a multi-element list,
   1162 with the components space-separated, use the dollar-caret
   1163 .Rc ( $^ )
   1164 operator:
   1165 .Ds
   1166 .Cr $^var
   1167 .De
   1168 .PP
   1169 This is useful when the normal list concatenation rules need to be
   1170 bypassed.
   1171 For example, to append a single period at the end of
   1172 .Cr $path ,
   1173 use:
   1174 .Ds
   1175 .Cr "echo $^path."
   1176 .De
   1177 .PP
   1178 For compatibility with the Plan 9 rc,
   1179 .Ds
   1180 .Cr $"var
   1181 .De
   1182 .PP
   1183 is accepted as a synonym for dollar-caret.
   1184 .SS "Backquote Substitution"
   1185 A list may be formed from the output of a command by using backquote
   1186 substitution:
   1187 .Ds
   1188 .Cr "\`{ command }"
   1189 .De
   1190 .PP
   1191 returns a list formed from the standard output of the command in braces.
   1192 .Cr $ifs
   1193 is used to split the output into list elements.
   1194 By default,
   1195 .Cr $ifs
   1196 has the value space-tab-newline.
   1197 .PP
   1198 The braces may be omitted if the command is a single word.
   1199 Thus
   1200 .Cr \`ls
   1201 may be used instead of
   1202 .Cr "\`{ls}" .
   1203 This last feature can be used to create shortcuts by defining functions that
   1204 expand to useful argument lists.
   1205 For example:
   1206 .Ds
   1207 .Cr "fn src { echo *.[chy] }"
   1208 .De
   1209 .PP
   1210 followed by
   1211 .Ds
   1212 .Cr "wc \`src"
   1213 .De
   1214 .PP
   1215 This will print out a word-count of all C source files in the current
   1216 directory.
   1217 .PP
   1218 In order to override the value of
   1219 .Cr $ifs
   1220 for a single backquote
   1221 substitution, use:
   1222 .Ds
   1223 .Cr "\`(ifs-list){ command }"
   1224 .De
   1225 .PP
   1226 .Cr $ifs
   1227 will be temporarily ignored and the command's output will be split as specified by
   1228 the list following the double backquote.
   1229 For example:
   1230 .Ds
   1231 .Cr "\`($nl :){cat /etc/passwd}"
   1232 .De
   1233 .PP
   1234 splits up
   1235 .Cr /etc/passwd
   1236 into fields.
   1237 .PP
   1238 As a convenience,
   1239 .I rc
   1240 defines
   1241 .Cr $nl
   1242 to contain the newline character, and
   1243 .Cr $tab
   1244 to contain the tab character. Thus, if you want to process everything in the
   1245 current directory, but in a random order, you could use:
   1246 .Ds
   1247 .Cr "for (f in \`$nl{ls | shuf}) { ... process $f }"
   1248 .De
   1249 .PP
   1250 This will correctly handle filenames that contain spaces.
   1251 .PP
   1252 Note that
   1253 .I rc
   1254 scripts that use backquote substitution should avoid relying on the default
   1255 values of
   1256 .Cr $ifs ,
   1257 .Cr $nl ,
   1258 or
   1259 .Cr $tab .
   1260 Instead, they should explicitly set what they need.
   1261 .SH "SPECIAL VARIABLES"
   1262 Several variables are known to
   1263 .I rc
   1264 and are treated specially.
   1265 In the following list, ``default'' indicates that
   1266 .I rc
   1267 gives the variable a default value on startup; ``no-export'' indicates
   1268 that the variable is never exported; and ``read-only'' indicates that
   1269 an attempt to set the variable will silently have no effect.
   1270 .PP
   1271 Also, ``alias'' means that the variable is aliased to the same name in
   1272 capitals.
   1273 For example, an assignment to
   1274 .Cr $cdpath
   1275 causes an automatic assignment to
   1276 .Cr $CDPATH ,
   1277 and vice-versa.
   1278 If
   1279 .Cr $CDPATH
   1280 is set when
   1281 .I rc
   1282 is started, its value is imported into
   1283 .Cr $cdpath .
   1284 .Cr $cdpath
   1285 and
   1286 .Cr $path
   1287 are
   1288 .I rc
   1289 lists;
   1290 .Cr $CDPATH
   1291 and
   1292 .Cr $PATH
   1293 are colon-separated lists.
   1294 Only the names spelt in capitals are exported into the environment.
   1295 .TP
   1296 .Cr * " (no-export)"
   1297 The argument list of
   1298 .IR rc .
   1299 .Cr "$1, $2,"
   1300 etc. are the same as
   1301 .Cr $*(1) ,
   1302 .Cr $*(2) ,
   1303 etc.
   1304 .TP
   1305 .Cr 0 " (default no-export)"
   1306 The variable
   1307 .Cr $0
   1308 holds the value of
   1309 .Cr argv[0]
   1310 with which
   1311 .I rc
   1312 was invoked.
   1313 Additionally,
   1314 .Cr $0
   1315 is set to the name of a function for the duration of
   1316 the execution of that function, and
   1317 .Cr $0
   1318 is also set to the name of the
   1319 file being interpreted for the duration of a
   1320 .Cr .
   1321 command.
   1322 .Cr $0
   1323 is not an element of
   1324 .Cr $* ,
   1325 and is never treated as one.
   1326 .TP
   1327 .Cr apid " (no-export)"
   1328 The process ID of the last process started in the background.
   1329 .TP
   1330 .Cr apids " (no-export read-only)"
   1331 A list whose elements are the process IDs of all background processes
   1332 which are still alive, or which have died and have not been waited for
   1333 yet.
   1334 .TP
   1335 .Cr bqstatus " (no-export)"
   1336 The exit status of the
   1337 .I rc
   1338 forked to execute the most recent backquote substitution. Note that, unlike
   1339 .Cr $status ,
   1340 .Cr $bqstatus
   1341 is always a single element list (see
   1342 .BR "EXIT STATUS"
   1343 below). For example:
   1344 .Ds
   1345 .Cr "echo foo |grep bar; whatis status"
   1346 .De
   1347 .TP
   1348 \&
   1349 prints
   1350 .Ds
   1351 .Cr "status=(0 1)"
   1352 .De
   1353 .TP
   1354 \&
   1355 whereas
   1356 .Ds
   1357 .Cr "x=`{echo foo |grep bar}; whatis bqstatus"
   1358 .De
   1359 .TP
   1360 \&
   1361 prints
   1362 .Ds
   1363 .Cr "bqstatus=1"
   1364 .De
   1365 .TP
   1366 .Cr cdpath " (alias)"
   1367 A list of directories to search for the target of a
   1368 .B cd
   1369 command.
   1370 The empty string stands for the current directory.
   1371 Note that if the
   1372 .Cr $cdpath
   1373 variable does not contain the current directory, then the current
   1374 directory will not be searched; this allows directory searching to
   1375 begin in a directory other than the current directory.
   1376 .TP
   1377 .Cr history
   1378 .Cr $history
   1379 contains the name of a file to which commands are appended as
   1380 .I rc
   1381 reads them.
   1382 This facilitates the use of a stand-alone history program
   1383 (such as
   1384 .IR history (1))
   1385 which parses the contents of the history file and presents them to
   1386 .I rc
   1387 for reinterpretation.
   1388 If
   1389 .Cr $history
   1390 is not set, then
   1391 .I rc
   1392 does not append commands to any file.
   1393 .TP
   1394 .Cr home " (alias)"
   1395 The default directory for the builtin
   1396 .B cd
   1397 command, and the directory in which
   1398 .I rc
   1399 looks to find its initialization file,
   1400 .Cr .rcrc ,
   1401 if
   1402 .I rc
   1403 has been started up as a login shell.
   1404 .TP
   1405 .Cr ifs " (default)"
   1406 The internal field separator, used for splitting up the output of
   1407 backquote commands for digestion as a list. On startup,
   1408 .I rc
   1409 assigns the list containing the characters space, tab, and newline to
   1410 .Cr $ifs .
   1411 .TP
   1412 .Cr nl " (default)"
   1413 Contains the newline character (see
   1414 .BR "Backquote substitution"
   1415 above).
   1416 .TP
   1417 .Cr path " (alias)"
   1418 This is a list of directories to search in for commands.
   1419 The empty string stands for the current directory.
   1420 If neither
   1421 .Cr $PATH
   1422 nor
   1423 .Cr $path
   1424 is set at startup time,
   1425 .Cr $path
   1426 assumes a default value suitable for your system.
   1427 This is typically
   1428 .Cr "(/usr/local/bin /usr/bin /usr/ucb /bin .)"
   1429 .TP
   1430 .Cr pid " (default no-export)"
   1431 On startup,
   1432 .Cr $pid
   1433 is initialized to the numeric process ID of the currently running
   1434 .IR rc .
   1435 .TP
   1436 .Cr prompt " (default)"
   1437 This variable holds the two prompts (in list form, of course) that
   1438 .I rc
   1439 prints.
   1440 .Cr $prompt(1)
   1441 is printed before each command is read, and
   1442 .Cr $prompt(2)
   1443 is printed when input is expected to continue on the next
   1444 line.
   1445 .I rc
   1446 sets
   1447 .Cr $prompt
   1448 to
   1449 .Cr "('; ' '')"
   1450 by default.
   1451 The reason for this is that it enables an
   1452 .I rc
   1453 user to grab commands from previous lines using a
   1454 mouse, and to present them to
   1455 .I rc
   1456 for re-interpretation; the semicolon
   1457 prompt is simply ignored by
   1458 .IR rc .
   1459 The null
   1460 .Cr $prompt(2)
   1461 also has its
   1462 justification: an
   1463 .I rc
   1464 script, when typed interactively, will not leave
   1465 .Cr $prompt(2) 's
   1466 on the screen,
   1467 and can therefore be grabbed by a mouse and placed
   1468 directly into a file for use as a shell script, without further editing
   1469 being necessary.
   1470 .TP
   1471 .Cr prompt " (function)"
   1472 If this function is defined, then it gets executed every time
   1473 .I rc
   1474 is about to print
   1475 .Cr "$prompt(1)" .
   1476 .TP
   1477 .Cr status " (no-export read-only)"
   1478 The exit status of the last command.
   1479 If the command exited with a numeric value, that number is the status.
   1480 If the command died with a signal, the status is the name of that signal;
   1481 if a core file was created, the string
   1482 .Rc `` +core ''
   1483 is appended.
   1484 The value of
   1485 .Cr $status
   1486 for a pipeline is a list, with one entry, as above, for each process
   1487 in the pipeline.
   1488 For example, the command
   1489 .Ds
   1490 .Cr "ls | wc"
   1491 .De
   1492 .TP
   1493 \&
   1494 usually sets
   1495 .Cr $status
   1496 to
   1497 .Cr "(0 0)" .
   1498 .TP
   1499 .Cr tab " (default)"
   1500 Contains the tab character (see
   1501 .BR "Backquote substitution"
   1502 above).
   1503 .TP
   1504 .Cr version " (default)"
   1505 On startup, the first element of this list variable is initialized to
   1506 a string which identifies this version of
   1507 .IR rc .
   1508 The second element is initialized to a string which can be found by
   1509 .IR ident (1)
   1510 and the
   1511 .I what
   1512 command of 
   1513 .IR sccs (1).
   1514 .SH FUNCTIONS
   1515 .I rc
   1516 functions are identical to
   1517 .I rc
   1518 scripts, except that they are stored
   1519 in memory and are automatically exported into the environment.
   1520 A shell function is declared as:
   1521 .Ds
   1522 .Cr "fn name { commands }"
   1523 .De
   1524 .PP
   1525 .I rc
   1526 scans the definition until the close-brace, so the function can
   1527 span more than one line.
   1528 The function definition may be removed by typing
   1529 .Ds
   1530 .Cr "fn name"
   1531 .De
   1532 .PP
   1533 (One or more names may be specified.
   1534 With an accompanying definition, all names receive the same definition.
   1535 This is sometimes useful
   1536 for assigning the same signal handler to many signals.
   1537 Without a definition, all named functions are deleted.)
   1538 When a function is executed,
   1539 .Cr $*
   1540 is set to the arguments to that
   1541 function for the duration of the command.
   1542 Thus a reasonable definition for
   1543 .Cr "l" ,
   1544 a shorthand for
   1545 .IR ls (1),
   1546 could be:
   1547 .Ds
   1548 .Cr "fn l { ls -FC $* }"
   1549 .De
   1550 .PP
   1551 but not
   1552 .Ds
   1553 .Cr "fn l { ls -FC } # WRONG"
   1554 .De
   1555 .SH "INTERRUPTS AND SIGNALS"
   1556 .I rc
   1557 recognizes a number of signals, and allows the user to define shell
   1558 functions which act as signal handlers.
   1559 .I rc
   1560 by default traps
   1561 .Cr SIGINT
   1562 when it is in interactive mode.
   1563 .Cr SIGQUIT
   1564 and
   1565 .Cr SIGTERM
   1566 are ignored, unless
   1567 .I rc
   1568 has been invoked with the
   1569 .Cr \-d
   1570 flag.
   1571 However, user-defined signal handlers may be written for these and
   1572 all other signals.
   1573 The way to define a signal handler is to
   1574 write a function by the name of the signal in lower case.
   1575 Thus:
   1576 .Ds
   1577 .Cr "fn sighup { echo hangup; rm /tmp/rc$pid.*; exit }"
   1578 .De
   1579 .PP
   1580 In addition to Unix signals,
   1581 .I rc
   1582 recognizes the artificial signal
   1583 .Cr SIGEXIT
   1584 which occurs as
   1585 .I rc
   1586 is about to exit.
   1587 .PP
   1588 In order to remove a signal handler's definition,
   1589 remove it as though it were a regular function.
   1590 For example:
   1591 .Ds
   1592 .Cr "fn sigint"
   1593 .De
   1594 .PP
   1595 returns the handler of
   1596 .Cr SIGINT
   1597 to the default value.
   1598 In order to ignore a signal, set the signal handler's value to
   1599 .Cr "{}" .
   1600 Thus:
   1601 .Ds
   1602 .Cr "fn sigint {}"
   1603 .De
   1604 .PP
   1605 causes
   1606 .Cr SIGINT
   1607 to be ignored by the shell.
   1608 Only signals that are being ignored are passed on to programs run by
   1609 .IR rc ;
   1610 signal functions are not exported.
   1611 .PP
   1612 On System V-based Unix systems,
   1613 .I rc
   1614 will not allow you to trap
   1615 .Cr SIGCLD .
   1616 .SH "BUILTIN COMMANDS"
   1617 Builtin commands execute in the context of the shell, but otherwise
   1618 behave exactly like other commands.
   1619 Although
   1620 .BR ! ,
   1621 .B ~
   1622 and
   1623 .B @
   1624 are not strictly speaking builtin commands,
   1625 they can usually be used as such.
   1626 .TP
   1627 \&\fB.\fR [\fB\-i\fR] \fIfile \fR[\fIarg ...\fR]
   1628 Reads
   1629 .I file
   1630 as input to
   1631 .IR rc
   1632 and executes its contents.
   1633 With a
   1634 .Cr \-i
   1635 flag, input is interactive.
   1636 Thus from within a shell script,
   1637 .Ds
   1638 .Cr ". \-i /dev/tty"
   1639 .De
   1640 .TP
   1641 \&
   1642 does the ``right thing''.
   1643 .TP
   1644 .B break
   1645 Breaks from the innermost
   1646 .Cr for
   1647 or
   1648 .Cr while ,
   1649 as in C.
   1650 It is an error to invoke
   1651 .B break
   1652 outside of a loop.
   1653 (Note that there is no
   1654 .B break
   1655 keyword between commands in
   1656 .Cr switch
   1657 statements, unlike C.)
   1658 .TP
   1659 \fBbuiltin \fIcommand \fR[\fIarg ...\fR]
   1660 Executes the command ignoring any function definition of the
   1661 same name.
   1662 This command is present to allow functions with the
   1663 same names as builtins to use the underlying builtin or binary.
   1664 For example:
   1665 .Ds
   1666 .Cr "fn ls { builtin ls -FC $* }"
   1667 .De
   1668 .TP
   1669 \&
   1670 is a reasonable way to pass a default set of arguments to
   1671 .Cr ls (1),
   1672 whereas
   1673 .Ds
   1674 .Cr "fn ls { ls -FC $* } # WRONG"
   1675 .De
   1676 .TP
   1677 \&
   1678 is a non-terminating recursion, which will cause
   1679 .Cr rc
   1680 to exhaust its stack space and (eventually) terminate if it is executed.
   1681 .TP
   1682 \fBcd \fR[\fIdirectory\fR]
   1683 Changes the current directory to
   1684 .IR directory .
   1685 The variable
   1686 .Cr $cdpath
   1687 is searched for possible locations of
   1688 .IR directory ,
   1689 analogous to the searching of
   1690 .Cr $path
   1691 for executable files.
   1692 With no argument,
   1693 .B cd
   1694 changes the current directory to
   1695 .Cr "$home" .
   1696 .TP
   1697 .B continue
   1698 Continues the innermost
   1699 .Cr for
   1700 or
   1701 .Cr while
   1702 loop,
   1703 as in C.
   1704 It is an error to invoke
   1705 .B continue
   1706 outside of a loop.
   1707 .TP
   1708 \fBecho \fR[\fB\-n\fR] [\fB\-\|\-\fR] [\fIarg ...\fR]
   1709 Prints its arguments to standard output, terminated by a newline.
   1710 Arguments are separated by spaces.
   1711 If the first argument is
   1712 .Cr "\-n"
   1713 no final newline is printed.
   1714 If the first argument is
   1715 .Cr "\-\|\-" ,
   1716 then all other arguments are echoed literally.
   1717 This is used for echoing a literal
   1718 .Cr "\-n" .
   1719 .TP
   1720 \fBeval \fR[\fIlist\fR]
   1721 Concatenates the elements of
   1722 .I list
   1723 with spaces and feeds the resulting string to
   1724 .I rc
   1725 for re-scanning.
   1726 This is the only time input is rescanned in
   1727 .IR rc .
   1728 .TP
   1729 \fBexec \fR[\fIarg ...\fR]
   1730 Replaces
   1731 .I rc
   1732 with the given command.
   1733 If the exec contains only redirections,
   1734 then these redirections apply to the current shell
   1735 and the shell does not exit.
   1736 For example,
   1737 .Ds
   1738 .Cr "exec >[2] err.out"
   1739 .De
   1740 .TP
   1741 \&
   1742 places further output to standard error in the file
   1743 .IR err.out .
   1744 .TP
   1745 \fBexit \fR[\fIstatus\fR]
   1746 Cause the current shell to exit with the given exit
   1747 .IR status .
   1748 If no argument is given, the current value of
   1749 .Cr $status
   1750 is used.
   1751 .TP
   1752 \fBflag\fR \fIf\fR [ \fB+\fR | \fB\-\fR ]
   1753 Test, set (\fB+\fR), or reset (\fB\-\fR) command-line flag
   1754 .IR f .
   1755 For example, a script that requires ``exit if command fails'' semantics can say
   1756 .Ds
   1757 .Cr "flag e +"
   1758 .De
   1759 .TP
   1760 \&
   1761 Some flags cannot be set or reset using
   1762 .BR flag ,
   1763 but they can still be tested. These are
   1764 .BR c ,
   1765 .BR d ,
   1766 .BR l ,
   1767 .BR o ,
   1768 .BR p ,
   1769 and
   1770 .BR s .
   1771 As a special case,
   1772 .Cr "flag i"
   1773 operates on
   1774 .BR rc 's
   1775 internal
   1776 .IR interactive
   1777 flag, which may have been set by
   1778 .Cr -i
   1779 on the command line, or if standard input was a terminal; there is no
   1780 .Cr "flag I" .
   1781 .TP
   1782 \fBlimit \fR[\fB\-h\fR] [\fIresource \fR[\fIvalue\fR]]
   1783 Similar to the
   1784 .IR csh (1)
   1785 .B limit
   1786 builtin, this command operates upon the
   1787 BSD-style resource limits of a process.
   1788 The
   1789 .Cr \-h
   1790 flag displays/alters the hard
   1791 limits.
   1792 The resources which can be shown or altered are
   1793 .BR cputime ,
   1794 .BR filesize ,
   1795 .BR datasize ,
   1796 .BR stacksize ,
   1797 .BR coredumpsize ,
   1798 .BR memoryuse ,
   1799 and, where supported,
   1800 .BR descriptors ,
   1801 .BR memoryuse ,
   1802 .BR memoryrss ,
   1803 .BR maxproc ,
   1804 .BR memorylocked ,
   1805 and
   1806 .BR filelocks .
   1807 For example:
   1808 .Ds
   1809 .Cr "limit coredumpsize 0"
   1810 .De
   1811 .TP
   1812 \&
   1813 disables core dumps.
   1814 To set a soft limit equal to the hard limit:
   1815 .Ds
   1816 .Cr "limit `{limit -h datasize}"
   1817 .De
   1818 .TP
   1819 .B newpgrp
   1820 Puts
   1821 .I rc
   1822 into a new process group.
   1823 This builtin is useful for making
   1824 .I rc
   1825 behave like a job-control shell in a hostile environment.
   1826 One example is the NeXT Terminal program, which implicitly assumes
   1827 that each shell it forks will put itself into a new process group.
   1828 .TP
   1829 \fBreturn \fR[\fIn\fR]
   1830 Returns from the current function, with status
   1831 .IR n ,
   1832 where
   1833 .IR n
   1834 is a valid exit status, or a list of them.
   1835 Thus it is legal to have
   1836 .Ds
   1837 .Cr "return (sigpipe 1 2 3)"
   1838 .De
   1839 .TP
   1840 \&
   1841 (This is commonly used to allow a function to return with the exit status
   1842 of a previously executed pipeline of commands.)
   1843 If
   1844 .IR n
   1845 is omitted, then
   1846 .Cr $status
   1847 is left unchanged.
   1848 It is an error to invoke
   1849 .B return
   1850 when not inside a function.
   1851 .TP
   1852 \fBshift \fR[\fIn\fR]
   1853 Deletes
   1854 .I n
   1855 elements from the beginning of
   1856 .Cr $*
   1857 and shifts the other
   1858 elements down by
   1859 .IR n .
   1860 .I n
   1861 defaults to 1.
   1862 .TP
   1863 \fBumask \fR[\fImask\fR]
   1864 Sets the current umask (see
   1865 .IR umask (2))
   1866 to the octal
   1867 .IR mask .
   1868 If no argument is present, the current mask value is printed.
   1869 .TP
   1870 \fBwait \fR[\fIpid\fR]
   1871 Waits for process with the specified
   1872 .IR pid ,
   1873 which must have been started by
   1874 .IR rc ,
   1875 to exit.
   1876 If no
   1877 .I pid
   1878 is specified,
   1879 .I rc
   1880 waits for all its child processes to exit.
   1881 .TP
   1882 \fBwhatis \fR[\fB\-b\fR] \fR[\fB\-f\fR] \fR[\fB\-p\fR] \fR[\fB\-s\fR] \fR[\fB\-v\fR] [\fB\-\|\-\fR] [\fIname ...\fR]
   1883 Prints a definition of the named objects.
   1884 For builtins,
   1885 .Cr builtin
   1886 .I foo
   1887 is printed; for functions, including signal handlers, their definitions
   1888 are printed; for executable files, path names are printed; and for
   1889 variables, their values are printed.
   1890 The flags restrict output to builtins, functions, executable
   1891 programs, signal handlers, and variables, respectively.
   1892 If no
   1893 .IR name s
   1894 are specified,
   1895 .I rc
   1896 lists all objects of that type.
   1897 (This is not permitted for
   1898 .Cr \-p .)
   1899 Without arguments,
   1900 .Cr whatis
   1901 is equivalent to
   1902 .Cr "whatis -fv" ,
   1903 and prints the values of all shell variables and functions.
   1904 .TP
   1905 \&
   1906 Note that
   1907 .B whatis
   1908 output is suitable for input to
   1909 .IR rc ;
   1910 by saving the output of
   1911 .B whatis
   1912 in a file, it should be possible to recreate the state of
   1913 .I rc
   1914 by sourcing this file with a
   1915 .Cr .
   1916 command.
   1917 Another note:
   1918 .Cr "whatis -s > file"
   1919 cannot be used to store the state of
   1920 .IR rc 's
   1921 signal handlers in a file, because builtins with redirections
   1922 are run in a subshell, and
   1923 .I rc
   1924 always restores signal handlers to their default value after a
   1925 .Cr fork() .
   1926 .TP
   1927 \&
   1928 Since
   1929 .B whatis
   1930 uses
   1931 .IR getopt (3)
   1932 to parse its arguments, you can use the special argument
   1933 .Cr "\-\|\-"
   1934 to terminate its flags.
   1935 This allows you to use names beginning with a dash, such as
   1936 the
   1937 .IR history (1)
   1938 commands.
   1939 For example,
   1940 .Ds
   1941 .Cr "whatis \-\|\- \-p"
   1942 .De
   1943 .SH EXAMPLES
   1944 The
   1945 .B shift
   1946 builtin only shifts
   1947 .Cr "$*" .
   1948 This function can shift any variable (except
   1949 .Cr "$_lshift" ).
   1950 .Ds
   1951 .Cr "fn lshift { _lshift=$* *=$$1 { shift $_lshift(2); $_lshift(1)=$* } }"
   1952 .De
   1953 .PP
   1954 With this definition in place,
   1955 .Ds
   1956 .Cr "walrus = (shoes ships sealing-wax cabbages kings)"
   1957 .Cr "lshift walrus 3"
   1958 .Cr "whatis walrus"
   1959 .De
   1960 .PP
   1961 prints
   1962 .Ds
   1963 .Cr "walrus=(cabbages kings)"
   1964 .De
   1965 .PP
   1966 The
   1967 .Cr $^var
   1968 operator flattens a list by separating each element with a space.
   1969 This function allows the separator to be an arbitrary string.
   1970 .Ds
   1971 .Cr "fn lflat {"
   1972 .Cr "  lflat=$*; *=$$1"
   1973 .Cr "  while () {"
   1974 .Cr "    echo -n $1; shift"
   1975 .Cr "    ~ $#* 0 && break"
   1976 .Cr "    echo -n $lflat(2)"
   1977 .Cr "  }"
   1978 .Cr "}"
   1979 .De
   1980 .PP
   1981 With this definition in place,
   1982 .Ds
   1983 .Cr "hops=(uunet mcvax ukc tlg)"
   1984 .Cr "lflat hops !"
   1985 .De
   1986 .PP
   1987 prints (with no final newline)
   1988 .Ds
   1989 .Cr uunet!mcvax!ukc!tlg
   1990 .De
   1991 .SH "EXIT STATUS"
   1992 The exit status of
   1993 .I rc
   1994 is normally the same as that of the last command executed.
   1995 If the
   1996 last command was a pipeline,
   1997 .I rc
   1998 exits
   1999 .Cr 0
   2000 if every command in the pipeline did; otherwise it exits
   2001 .Cr 1 .
   2002 .PP
   2003 .I rc
   2004 can be made to exit with a particular status using the
   2005 .B exit
   2006 builtin.
   2007 .SH "LINE EDITING"
   2008 .I rc
   2009 is typically built against a line editing library.
   2010 On GNU/Linux systems this will usually be the GNU
   2011 .I readline
   2012 library.
   2013 On *BSD systems it is more likely to be the BSD
   2014 .I editline
   2015 library.
   2016 Please consult the appropriate library documentation for details of how to use
   2017 and configure line editing.
   2018 .SS "Tilde Expansion"
   2019 Since
   2020 .I rc
   2021 does not support tilde expansion (converting
   2022 .Cr ~foo
   2023 to the home directory of user
   2024 .Cr foo ),
   2025 it is sometimes suggested by users as a
   2026 possible enhancement.
   2027 The authors and maintainers of
   2028 .I rc
   2029 have a proud history of resisting such feature requests.
   2030 So it is worth noting here that GNU
   2031 .I readline
   2032 can expand tildes.
   2033 Add this line to the file
   2034 .Cr .inputrc
   2035 in your home directory.
   2036 .Ds
   2037 .Cr "set expand-tilde on"
   2038 .De
   2039 .PP
   2040 and then use the key sequence
   2041 .Cr "M-~"
   2042 to perform tilde expansion on the current word.
   2043 See the
   2044 .I readline
   2045 documentation for further details.
   2046 .SH GRAMMAR
   2047 Here is
   2048 .IR rc 's
   2049 grammar, edited to remove semantic actions.
   2050 .Ds
   2051 .ft \*(Cf
   2052 %term ANDAND BACKBACK BANG CASE COUNT DUP ELSE END FLAT FN FOR IF IN
   2053 %term OROR PIPE REDIR SUB SUBSHELL SWITCH TWIDDLE WHILE WORD HUH
   2054 
   2055 %left '^' '='
   2056 %left WHILE ')' ELSE
   2057 %left ANDAND OROR '\en'
   2058 %left BANG SUBSHELL
   2059 %left PIPE
   2060 %right '$'
   2061 %left SUB
   2062 
   2063 %start rc
   2064 
   2065 %%
   2066 
   2067 rc: line end
   2068 	| error end
   2069 
   2070 end: END /* EOF */ | '\en'
   2071 
   2072 cmdsa: cmd ';' | cmd '&'
   2073 
   2074 line: cmd | cmdsa line
   2075 
   2076 body: cmd | cmdsan body
   2077 
   2078 cmdsan: cmdsa | cmd '\en'
   2079 
   2080 brace: '{' body '}'
   2081 
   2082 paren: '(' body ')'
   2083 
   2084 assign: first optcaret '=' optcaret word
   2085 
   2086 epilog: /* empty */ | redir epilog
   2087 
   2088 redir: DUP | REDIR word
   2089 
   2090 case: CASE words ';' | CASE words '\en'
   2091 
   2092 cbody: cmd | case cbody | cmdsan cbody
   2093 
   2094 iftail: cmd	%prec ELSE
   2095 	| brace ELSE optnl cmd
   2096 
   2097 cmd	: /* empty */	%prec WHILE
   2098 	| simple
   2099 	| brace epilog
   2100 	| IF paren optnl iftail
   2101 	| FOR '(' word IN words ')' optnl cmd
   2102 	| FOR '(' word ')' optnl cmd
   2103 	| WHILE paren optnl cmd	
   2104 	| SWITCH '(' word ')' optnl '{' cbody '}'
   2105 	| TWIDDLE optcaret word words
   2106 	| cmd ANDAND optnl cmd
   2107 	| cmd OROR optnl cmd
   2108 	| cmd PIPE optnl cmd
   2109 	| redir cmd	%prec BANG
   2110 	| assign cmd	%prec BANG
   2111 	| BANG optcaret cmd
   2112 	| SUBSHELL optcaret cmd
   2113 	| FN words brace
   2114 	| FN words
   2115 
   2116 optcaret: /* empty */ %prec '^' | '^'
   2117 
   2118 simple: first | first args
   2119 
   2120 args: arg | args arg
   2121 
   2122 arg: word | redir
   2123 
   2124 first: comword | first '^' sword
   2125 
   2126 sword: comword | keyword
   2127 
   2128 word: sword | word '^' sword
   2129 
   2130 comword: '$' sword
   2131 	| '$' sword SUB words ')'
   2132 	| COUNT sword
   2133 	| FLAT sword
   2134 	| '`' sword
   2135 	| '`' brace
   2136 	| BACKBACK word	brace | BACKBACK word sword
   2137 	| '(' words ')'
   2138 	| REDIR brace
   2139 	| WORD
   2140 
   2141 keyword: FOR | IN | WHILE | IF | SWITCH
   2142 	| FN | ELSE | CASE | TWIDDLE | BANG | SUBSHELL | '='
   2143 
   2144 words: /* empty */ | words word
   2145 
   2146 optnl: /* empty */ | optnl '\en'
   2147 .ft R
   2148 .De
   2149 .SH FILES
   2150 .Cr $HOME/.rcrc ,
   2151 .Cr /tmp/rc* ,
   2152 .Cr /dev/null
   2153 .SH CREDITS
   2154 .I rc
   2155 was written by Byron Rakitzis, with valuable help
   2156 from Paul Haahr, Hugh Redelmeier and David Sanderson.
   2157 The design of this shell was copied from the
   2158 .I rc
   2159 that Tom Duff wrote at Bell Labs.
   2160 .SH BUGS
   2161 There is a compile-time limit on the number of
   2162 .Cr ;
   2163 separated commands in a line: usually 500.
   2164 This is sometimes a problem for automatically generated scripts:
   2165 substituting the newline character for
   2166 .Cr ;
   2167 avoids the limit.
   2168 .PP
   2169 On modern systems that support
   2170 .Cr /dev/fd
   2171 or
   2172 .Cr /proc/self/fd ,
   2173 .Cr <{foo}
   2174 style redirection is implemented that way.
   2175 However, on older systems it is implemented with named pipes.
   2176 Allegedly, it is sometimes possible to foil
   2177 .I rc
   2178 into removing the FIFO it places in
   2179 .Cr /tmp
   2180 prematurely, or it is even possible to cause
   2181 .I rc
   2182 to hang.
   2183 (The current maintainer has never seen this, but then he
   2184 doesn't use systems which lack
   2185 .Cr /dev/fd
   2186 any more.
   2187 If anybody can reproduce this problem, please let the maintainer know.)
   2188 .PP
   2189 The
   2190 .B echo
   2191 command does not need to be a builtin. It is one for reasons of
   2192 performance and portability (of
   2193 .I rc
   2194 scripts).
   2195 .PP
   2196 There should be a way to avoid exporting a variable.
   2197 .PP
   2198 Extra parentheses around a
   2199 .Cr ~
   2200 expression or a
   2201 .Cr !
   2202 expression are a syntax error.
   2203 Thus, this code is illegal.
   2204 .Ds
   2205 .Cr "while ((~ $1 -*) && (! ~ $1 --)) { ..."
   2206 .De
   2207 .TP
   2208 The redundant inner parentheses must be omitted.
   2209 .PP
   2210 Variable subscripting cannot be used in here documents.
   2211 .PP
   2212 The
   2213 .Cr limit
   2214 builtin silently ignores extra arguments.
   2215 .PP
   2216 Backquote substitution never produces empty strings - multiple
   2217 consecutive occurrences of the separator are treated the same as a
   2218 single occurrence.
   2219 .Ds
   2220 .Cr "ifs=! { x = `{echo -n a!!b}; whatis x }"
   2221 .Cr "x=(a b) # NOT x=(a '' b)"
   2222 .PP
   2223 Bug reports should be mailed to
   2224 .Cr "<toby@paccrat.org>" .
   2225 .SH INCOMPATIBILITIES
   2226 Here is a list of features which distinguish this incarnation of
   2227 .I rc
   2228 from the one described in the Bell Labs manual pages:
   2229 .PP
   2230 The Tenth Edition
   2231 .I rc
   2232 does not have the
   2233 .B else
   2234 keyword.
   2235 Instead,
   2236 .B if
   2237 is optionally followed by
   2238 an
   2239 .B "if not"
   2240 clause which is executed
   2241 if the preceding
   2242 .B if
   2243 test does not succeed.
   2244 .PP
   2245 Backquotes are slightly different in Tenth Edition
   2246 .IR rc :
   2247 a backquote must always be followed by a left-brace.
   2248 This restriction is not present for single-word commands in this
   2249 .IR rc .
   2250 .PP
   2251 For
   2252 .Cr .
   2253 .IR file ,
   2254 the Tenth Edition
   2255 .IR rc
   2256 searches
   2257 .Cr $path
   2258 for
   2259 .IR file .
   2260 This
   2261 .I rc
   2262 does not, since it is not considered useful.
   2263 .PP
   2264 The list flattening operator,
   2265 .Cr $^foo ,
   2266 is spelt
   2267 .Cr "$""foo"
   2268 in those versions of the Bell Labs
   2269 .IR rc
   2270 which have it.
   2271 .PP
   2272 The following are all new with this version of
   2273 .IR rc :
   2274 The
   2275 .Cr \-n
   2276 flag,
   2277 here strings (they facilitate exporting of functions
   2278 with here documents into the environment),
   2279 the
   2280 .B return
   2281 and
   2282 .B break
   2283 keywords,
   2284 the
   2285 .B echo
   2286 builtin,
   2287 the
   2288 .Cr bqstatus
   2289 and
   2290 .Cr version
   2291 variables,
   2292 the
   2293 .Cr prompt
   2294 function,
   2295 support for GNU
   2296 .IR readline
   2297 and other line editing libraries.
   2298 This
   2299 .I rc
   2300 also sets
   2301 .Cr $0
   2302 to the name of a function being executed/file
   2303 being sourced.
   2304 .SH "SEE ALSO"
   2305 ``rc \(em A Shell for Plan 9 and UNIX Systems'',
   2306 Unix Research System,
   2307 Tenth Edition,
   2308 Volume 2. (Saunders College Publishing)
   2309 .PP
   2310 .Cr http://static.tobold.org/rc/rc-duff.html ,
   2311 an updated version of the above paper.
   2312 .PP
   2313 .IR history (1)