RC

Section: User Commands (1)
Updated: 28 April 1991
Index Return to Main Contents
 

NAME

rc - shell  

SYNOPSIS

rc [ -eixvld ] [ -c command ] [ arguments ]  

DESCRIPTION

rc is a command interpreter and programming language similar to sh(1). It is based on the AT&T plan 9 shell of the same name. The shell offers a C-like syntax (much more so than the C shell), and a powerful mechanism for manipulating variables. It is reasonably small and reasonably fast, especially when compared to contemporary shells. Its use is intended to be interactive, but the language lends itself well to scripts.  

OPTIONS

-e
If the C-e option is present, then rc will exit if the exit status of a command is false (nonzero). rc will not exit, however, if a conditional fails, e.g., an Cif() command.
-i
If the C-i option is present or if the input to rc is from a terminal (as determined by isatty(3)) then rc will be in interactive mode. That is, a prompt (from C$prompt(1) will be printed before an input line is taken, and rc will ignore the signals CSIGINT and CSIGQUIT.
-x
This option will make rc print every command on standard error before it is executed. It can be useful for debugging rc scripts.
-v
This option will echo input to rc on standard error as it is read.
-l
If the C-l option is present, or if rc's Cargv[0][0] is a dash (C-), then rc will behave as a login shell. That is, it will try to run commands present in C$home/.rcrc, if this file exists, before reading any other input.
-d
This flag causes rc not to trap CSIGQUIT, and thus rc will dump core when it receives this signal. This option is only useful for debugging rc.
-c
If C-c is present, commands are executed from the immediately following argument. Any further arguments to rc are placed in C$*.

 

COMMANDS

A simple command is a sequence of words, separated by white space (space and tab) characters that ends with a newline, semicolon (C;), or ampersand (C&). The first word of a command is the name of that command. If the name begins with C/, C./, or C../, then the name is used as an absolute path name referring to an executable file. Otherwise, the name of the command is looked up in a table of shell functions, builtin commands, or as a file in the directories named by C$path.  

Background Tasks

A command ending with a C& is run in the background; that is, the shell returns immediately rather than waiting for the command to complete. Background commands have /dev/null connected to their standard input unless an explicit redirection for standard input is used.  

Subshells

A command prefixed with an at-sign (C@) is executed in a subshell. This insulates the parent shell from the effects of a cd or a variable assignment. For example:

C@ {cd ..; make}

will run make(1) in the parent directory (C..), but leaves the shell running in the current directory.  

Line continuation

A long logical line may be continued over several physical lines by terminating each line (except the last) with a backslash (C\). The backslash-newline sequence is treated as a space. A backslash is not otherwise special to rc.  

Quoting

rc interprets several characters specially; special characters automatically terminate words. The following characters are special:

C# ; & | ^ $ = ` ' { } ( ) < >

The single quote (C') prevents special treatment of any character other than itself. All characters, including control characters, newlines, and backslashes between two quote characters are treated as an uninterpreted string. A quote character itself may be quoted by placing two quotes in a row. The minimal sequence needed to enter the quote character is C''''. The empty string is represented by C''. Thus:

Cecho 'What''s the plan, Stan?'

prints out

CWhat's the plan, Stan?

 

Grouping

Zero or more commands may be grouped within braces (`C{' and `C}'), and are then treated as one command. Braces do not otherwise define scope; they are used only for command grouping. In particular, be wary of the command:

Cfor (i) {

C       command

C} | command

Since pipe binds tighter than for, this command does not perform what the user expects it to. Instead, enclose the whole for statement in braces:

C{for (i) command} | command

Fortunately, rc's grammar is simple enough that a (confident) user can understand it by examining the skeletal yacc grammar at the end of this man page (see the section entitled GRAMMAR).  

Input and output

The standard output may be redirected to a file with

Ccommand > file

and the standard input may be taken from a file with

Ccommand < file

File descriptors other than 0 and 1 may be specified also. For example, to redirect standard error to a file, use:

Ccommand >[2] file

In order to duplicate a file descriptor, use C>[nC=mC]. Thus to redirect both standard output and standard error to the same file, use

Ccommand > file >[2=1]

To close a file descriptor that may be open, use C>[nC=]. For example, to close file-descriptor 7:

Ccommand >[7=]

In order to place the output of a command at the end of an already existing file, use:

Ccommand >> file

If the file does not exist, then it is created.

``Here documents'' are supported as in sh(1) with the use of

Ccommand << 'eof-marker'

If the end-of-file marker is enclosed in quotes, then no variable substitution occurs inside the here document. Otherwise, every variable is substituted by its space-separated-list value (see Flat Lists, below), and if a C^ character follows a variable name, it is deleted. This allows the unambiguous use of variables adjacent to text, as in

C$variable^follow

Additionally, rc supports ``here strings'', which are like here documents, except that input is taken directly from a string on the command line. Its use is illustrated here:

Ccat <<< 'this is a here string' | wc

(This feature enables rc to export functions using here documents into the environment; the author does not expect users to find this feature useful.)  

Pipes

Two or more commands may be combined in a pipeline by placing the vertical bar (C|) between them. The standard output (file descriptor 1) of the command on the left is tied to the standard input (file descriptor 0) of the command on the right. The notation C|[nC=mC] indicates that file descriptor n of the left process is connected to file descriptor m of the right process. C|[nC] is a shorthand for C|[nC=0]. As an example, to pipe the standard error of a command to wc(1), use:

Ccommand |[2] wc

 

Commands as Arguments

Some commands, like cmp(1) or diff(1), take their arguments on the command line, and do not read input from standard input. It is convenient sometimes to build nonlinear pipelines so that a command like cmp can read the output of two other commands at once. rc does it like this:

Ccmp <{command} <{command}

compares the output of the two commands in braces. A note: since this form of redirection is implemented with named pipes, and since one cannot lseek(2) on a pipe, commands that use lseek(2) will hang. For example, most versions of diff(1) use lseek(2) on their inputs.  

CONTROL STRUCTURES

The following may be used for control flow in rc:  

If-else Statements

if (test) {
cmd

} else cmd
The test is executed, and if its return status is zero, the first command is executed, otherwise the second is. Braces are not mandatory around the commands. However, an else statement is valid only if it follows a close-brace on the same line. Otherwise, the if is taken to be a simple-if:

Cif (test)

C       command

 

While and For Loops

while (test) cmd
rc executes the test and performs the command as long as the test is true.
for (var in list) cmd
rc sets var to each element of list (which may contain variables and backquote substitutions) and runs cmd. If ``in list'' is omitted, then rc will set var to each element of C$* (excluding C$0. For example:

Cfor (i in `{ls -F | grep '\*$' | sed 's/\*$//'}) { commands }

will set C$i to the name of each file in the current directory which is executable.  

Switch

switch (list) { case ... }
rc looks inside the braces after a switch statement for single lines beginning with the word case. If any of the patterns following case match the list supplied to switch, then the commands up until the next case statement are executed. Metacharacters should not be quoted; matching is performed only against the strings in list, not against file names. (Matching for case statements is the same as for the C~ command.)
 

Logical Operators

There are a number of operators in rc which depend on the exit status of a command.

Ccommand && command

executes the first command and then executes the second command if and only if the first command exits with a zero exit status (``true'' in UNIX).

Ccommand || command

executes the first command executing the second command if and only if the second command exits with a nonzero exit status (``false'' in UNIX).

C! command

negates the exit status of a command. Thus:

C! command || command

is equivalent to

Ccommand && command

 

PATTERN MATCHING

There are two forms of pattern matching in rc. One is traditional shell globbing. This occurs in matching for file names in argument lists:

Ccommand argument argument ...

When the characters C*, C[ or C? occur in an argument, rc looks at the argument as a pattern for matching against files according to the following rules: a C* matches any number (including zero) of characters. A C? matches any single character, and a C[ followed by a number of characters followed by a C] matches a single character in that class. The rules for character class matching are the same as those for ed(1), with the exception that character class negation is achieved with the tilde (C~), not the caret (C^), since the caret already means something else in rc.

rc also matches patterns against strings with the C~ command:

C~ subject pattern pattern ...

C~ sets C$status to zero if and only if a supplied pattern matches any single element of the subject list. Thus

C~ foo f*

sets status to zero, while

C~ (bar baz) f*

sets status to one. The null list is matched by the null list, so

C~ $foo ()

checks to see whether C$foo is empty or not. This may also be achieved by the test

C~ $#foo 0

Note that inside a C~ command rc does not match patterns against file names, so it is not necessary to quote the characters C*, C[ and C?. Finally, note that if the C~ command is given a list as its first argument, then a successful match against any of the elements of that list will cause C~ to return true. For example:

C~ (foo goo zoo) z*

is true.  

LISTS AND VARIABLES

The primary data structure in rc is the list, which is a sequence of words. Parentheses are used to group lists. The empty list is represented by C(). Lists have no hierarchical structure; a list inside another list is expanded so the outer list contains all the elements of the inner list. Thus, the following are all equivalent

Cone two three


C(one two three)


C((one) () ((two three)))

Note that the null string, C'', and the null list, C(), are two very different things. Assigning the null string to variable is a valid operation, but it does not remove its definition. For example, if C$a is set to C'', then C$#a, returns a 1.  

List Concatenation

Two lists may be joined by the concatenation operator (C^). A single word is treated as a list of length one, so

Cecho foo^bar

produces the output

Cfoobar

For lists of more than one element, concatenation works according to the following rules: if the two lists have the same number of elements, then concatenation is pairwise:

Cecho (a- b- c-)^(1 2 3)

produces the output

Ca-1 b-2 c-3

Otherwise, one of the lists must have a single element, and then the concatenation is distributive:

Ccc -^(O g c) (malloc alloca)^.c

has the effect of performing the command

Ccc -O -g -c malloc.c alloca.c

 

Free Carets

rc inserts carets (concatenation operators) for free in certain situations, in order to save some typing on the user's behalf. For example, the above example could also be typed in as:

Copts=(O g c) files=(malloc alloca) cc -$opts $files.c

rc takes care to insert a free-caret between the `C-' and C$opts, as well as between C$files and C.c. The rule for free carets is as follows: if a word or keyword is immediately followed by another word, keyword, dollar-sign or backquote, then rc inserts a caret between them.  

Variables

A list may be assigned to a variable, using the notation:

Cvar = list

Any sequence of non-special characters, except a sequence including only digits, may be used as a variable name. All user-defined variables are exported into the environment.

The value of a variable is referenced with the notation:

C$var

Any variable which has not been assigned a value returns the null list, C(), when referenced. In addition, multiple references are allowed:

Ca=foo

Cb=a

Cecho$$b

prints

Cfoo

A variable's definition may also be removed by assigning the null list to a variable:

Cvar=()

For ``free careting'' to work correctly, rc must make certain assumptions about what characters may appear in a variable name. rc currently assumes that a variable name consists only of alphanumeric characters, underscore (C_) and star (C*). To reference a variable with other characters in its name, quote the variable name. Thus:

Cecho $'we$IrdVariab!le'

 

Local Variables

Any number of variable assignments may be made local to a single command by typing:

Ca=foo b=bar ... command

The command may be a compound command, so for example:

Cpath=. ifs=() {

C...

sets path to C. and ifs to C() for the duration of one long compound command.  

Variable Subscripts

Variables may be subscripted with the notation

C$var(nC)

where n is a list of integers (origin 1). The list of subscripts need not be in order or even unique. Thus, if

Ca=(one two three)

then

Cecho $a(3 3 3)

prints

Cthree three three

If n references a nonexistent element, then C$var(nC) returns the null list. The notation C$n, where n is an integer, is a shorthand for C$*(nC). Thus, rc's arguments may be referred to as C$1, C$2, and so on.

Note also that the list of subscripts may be given by any of rc's list operations:

C$var(`{awk 'BEGIN{for(i=1;i<=10;i++)print i;exit; }'})

returns the first 10 elements of C$var.

To count the number of elements in a variable, use

C$#var

This returns a single-element list, with the number of elements in C$var.  

Flat Lists

In order to create a single-element list from a multi-element list, with the components space-separated, use

C$^var

This is useful when the normal list concatenation rules need to be bypassed. For example, to append a single period at the end of C$path, use:

Cecho $^path.

 

Backquote Substitution

A list may be formed from the output of a command by using backquote substitution:

C`{ command }

returns a list formed from the standard output of the command in braces. C$ifs is used to split the output into list elements. By default, C$ifs has the value space-tab-newline. The braces may be omitted if the command is a single word. Thus C`ls may be used instead of C`{ls}. This last feature is useful when defining functions that expand to useful argument lists. A frequent use is:

Cfn src { echo *.[chy] }

followed by

Cwc `src

(This will print out a word-count of all C source files in the current directory.)

In order to override the value of C$ifs for a single backquote substitution, use:

C`` (ifs-list) { command }

C$ifs will be temporarily ignored and the command's output will be split as specified by the list following the double backquote. For example:

C`` ($nl :) {cat /etc/passwd}

splits up /etc/passwd into fields, assuming that C$nl contains a newline as its value.  

SPECIAL VARIABLES

Several variables are known to rc and are treated specially.
*
The argument list of rc. C$1, $2, etc. are the same as C$*(1), C$*(2), etc. The variable C$0 holds the value of Cargv[0] with which rc was invoked. Additionally, C$0 is set to the name of a function for the duration of the execution of that function, and C$0 is also set to the name of the file being interpreted for the duration of a C. command.
apid
The process ID of the last process started in the background.
cdpath
A list of directories to search for the target of a cd command. The empty string stands for the current directory. Note that an assignment to C$cdpath causes an automatic assignment to C$CDPATH, and vice-versa
history
C$history contains the name of a file to which commands are appended as rc reads them. This facilitates the use of a stand-alone history program which parses the contents of the history file and presents them to rc for reinterpretation. If C$history is not set, then rc does not append commands to any file.
home
The default directory for the builtin cd command and is the directory in which rc looks to find its initialization file, .rcrc, if rc has been started up as a login shell. Like C$cdpath and C$CDPATH, C$home and C$HOME are aliased to each other.
ifs
The internal field separator, used for splitting up the output of backquote commands for digestion as a list.
path
This is a list of directories to search in for commands. The empty string stands for the current directory. Note that like C$cdpath and C$CDPATH, C$path and C$PATH are aliased to each other.
pid
The process ID of the currently running rc.
prompt
This variable holds the two prompts (in list form, of course) that rc prints. C$prompt(1) is printed before each command is read, and C$prompt(2) is printed when input is expected to continue on the next line. rc sets C$prompt to C('; ' '') by default. The reason for this is that it enables an rc user to grab commands from previous lines using a mouse, and to present them to rc for re-interpretation; the semicolon prompt is simply ignored by rc. The null C$prompt(2) also has its justification: an rc script, when typed interactively, will not leave C$prompt(2)'s on the screen, and can therefore be grabbed by a mouse and placed directly into a file for use as a shell script, without further editing being necessary.
prompt (function)
If this function is set, then it gets executed every time rc is about to print C$prompt(1).
status
The exit status of the last command. If the command exited with a numeric value, that number is the status. If the died with a signal, the status is the name of that signal; if a core file was created, the string ``C+core'' is appended. The value of C$status for a pipeline is a list, with one entry, as above, for each process in the pipeline. For example, the command 1i

Cls | wc

usually sets C$status to C(0 0).

The values of C$path, C$cdpath, and C$home are derived from the environment values of C$PATH, C$CDPATH, and C$HOME. Otherwise, they are derived from the environment values of C$path, C$cdpath and C$home. This is for compatibility with other UNIX programs, like sh(1). C$PATH and C$CDPATH are assumed to be colon-separated lists.  

FUNCTIONS

rc functions are identical to rc scripts, except that they are stored in memory and are automatically exported into the environment. A shell function is declared as:

Cfn name { commands }

rc scans the definition in until the close-brace, so the function can span more than one line. The function definition may be removed by typing

Cfn name

When a function is executed, C$* is set to the arguments to that function for the duration of the command. Thus a reasonable definition for Cl, a shorthand for ls(1), could be:

Cfn l { ls -FC $* }

but not

Cfn l { ls -FC }

 

INTERRUPTS AND SIGNALS

rc recognizes a number of signals, and allows the user to define shell functions which act as signal handlers. rc by default traps CSIGINT and CSIGQUIT when it is interactive mode. CSIGQUIT is ignored, unless rc has been invoked with the C-d flag. However, user-defined signal handlers may be written for these and all other signals. The way to define a signal handler is to write a function by the name of the signal in lower case. Thus:

Cfn sighup { echo hangup; rm /tmp/rc$pid.*; exit }

In addition to Unix signals, rc recognizes the artificial signal CSIGEXIT which occurs as rc is about to exit.

In order to remove a signal handler's definition, remove it as though it were a regular function. For example:

Cfn sigint

returns the handler of CSIGINT to the default value. In order to ignore a signal, set the signal handler's value to C{}. Thus:

Cfn sigint {}

causes SIGINT to be ignored by the shell.  

BUILTIN COMMANDS

Builtin commands execute in the context of the shell, but otherwise behave exactly like other commands.
. [-i] file args
Reads file as input to rc and executes its contents. With a C-i flag, input is interactive. Thus from within a shell script, 1i

C. -i /dev/tty

does the ``right'' thing.
break
Breaks from the innermost for or while, as in C. It is an error to invoke break outside of a loop. (Note that there is no break keyword between commands in switch statements, unlike C.)
builtin command ...
Executes the command as a builtin; no function lookup or directory searching is done. This command is present to allow functions with the same names as builtins to use the underlying builtin.
cd directory
Change the current directory. The variable C$cdpath is searched for possible locations of directory, analogous to the searching of C$path for executable files. With no argument, cd changes directory to C$home.
echo args ...
Prints its arguments to standard output. Arguments are separated by spaces. If the first argument is C-n, no final newline is printed. If the first argument is C--, then any subsequent arguments are ignored.
eval list
eval concatenates the elements of list with spaces and feeds the resulting string to rc for re-scanning. This is the only time input is rescanned in rc.
exec command
replaces rc with the given command. If the exec contains only redirections, then these redirections apply to the current shell and the shell does not exit. For example, 1i

Cexec >[2] err.out

places further output to standard error in the file err.out.
exit status
Cause the current shell to exit with the given exit status. If no argument is given, the current value of C$status is used.
limit [-h] [resource] [value]
Similar to the csh(1) limit builtin, this command operates upon the BSD-style limits of a process. The C-h flag displays/alters the hard limits. The resources which can be shown or altered are cputime, filesize, datasize, stacksize, coredumpsize and memoryuse. For example: 1i

Climit coredumpsize 0

disables core dumps.
return n
Returns from the current function, with status n. If n is omitted, then C$status is left unchanged. It is an error to invoke return when not inside a function.
shift n
Deletes n elements from the beginning of C$* and shifts the other elements down by n. n defaults to 1. (Note that C$0 is not affected by shift.)
umask mask
Sets the current umask (see umask(2)) to the octal mask. If no argument is present, the current mask value is printed.
wait pid
Waits for the specified pid, which must have been started by rc. If no pid is specified, rc waits for any child process to exit.
whatis name ...
Prints a definition of the named objects. For variables, their values are printed; for functions, their definitions are; and for executable files, path names are printed. Without arguments, whatis prints the values of all shell variables and functions. Note that whatis output is suitable for input to rc; by saving the output of whatis in a file, it should be possible to recreate the state of rc by sourcing this file with a C. command.
 

GRAMMAR

Here is rc's grammar, edited to remove semantic actions.

C
%term BANG DUP ELSE END FN FOR HUH IF IN LBRACK PIPE RBRACK
%term REDIR STAR SUB SUBSHELL SWITCH TWIDDLE WHILE WORD

%left IF WHILE FOR SWITCH ')' ELSE
%left ANDAND OROR
%left BANG SUBSHELL
%left PIPE
%left '^'
%right '$' COUNT FLAT
%left SUB
%left '`' BACKBACK

%%

rc: line end | error end

end: '\n' | END

cmdsa: cmd ';' | cmd '&'

line: cmd | cmdsa line

body: cmd | cmdsan body

cmdsan: cmdsa | cmd '\n'

brace: '{' body '}'

paren: '(' body ')'

assign: first '=' word

epilog: /* empty */ | redir epilog

redir: DUP | REDIR word

iftail: cmd %prec IF | brace ELSE cmd

cmd     : /* empty */
        | simple
        | brace epilog
        | IF paren { skipnl(); } iftail
        | FOR '(' word IN words ')' { skipnl(); } cmd
        | FOR '(' word ')' { skipnl(); } cmd
        | WHILE paren { skipnl(); } cmd
        | SWITCH '(' word ')' { skipnl(); } brace
        | TWIDDLE word words
        | cmd ANDAND { skipnl(); } cmd
        | cmd OROR { skipnl(); } cmd
        | cmd PIPE { skipnl(); } cmd
        | redir cmd %prec BANG
        | assign cmd %prec BANG
        | BANG cmd
        | SUBSHELL cmd
        | FN words brace
        | FN words

simple: first | simple word | simple redir

first: comword | first '^' word

word: comword | keyword | word '^' word

comword: WORD
        | COUNT word | FLAT word
        | '`' word | '`' brace
        | BACKBACK word brace | BACKBACK word word
        | '(' words ')'
        | REDIR brace
        |  '$' word | '$' word SUB words ')'

keyword: FOR | IN | WHILE | IF | SWITCH
        | FN | ELSE | TWIDDLE | BANG | SUBSHELL

words: /* empty */ | words word


 

FILES

$HOME/.rcrc, /tmp/rc*, /dev/null  

CREDITS

rc was written by Byron Rakitzis, with valuable help from Paul Haahr, Hugh Redelmeier and David Sanderson. The design of this shell has been copied from the rc that Tom Duff wrote at Bell Labs.  

BUGS

C<{foo} style redirection is implemented with named pipes, and it is sometimes possible to foil rc into removing the FIFO it places in /tmp prematurely, or it is even possible to cause rc to hang. This redirection should be implemented via /dev/fd on systems which have it. The functionality of shift should be available for variables other than C$*.

echo is built in only for performance reasons, which is a bad idea.

There should be a way to avoid exporting a variable.

The C$^var notation for flattening should allow for using an arbitrary separating character, not just space.

Bug reports should be mailed to Cbyron@archone.tamu.edu.  

INCOMPATIBILITIES

Here is a list of features which distinguish this incarnation of rc from the one described in the Bell Labs manual pages:

The treatment of if-else is different in the v10 rc: that version uses an ``if not'' clause which gets executed if the preceding ``if'' test does not succeed.

Backquotes are slightly different in v10 rc: a backquote must always be followed by a left-brace. This restriction is not present for single-word commands in this rc.

The following are all new with this version of rc: The list flattening operator, here strings (they facilitate exporting of functions with here documents into the environment), the return and break keywords, the echo builtin, the support for the GNU readline(3) library and the support for the prompt function. This rc also sets C$0 to the name of a function being executed/file being sourced.  

SEE ALSO

``rc --- A shell for Plan 9 and UNIX'', Unix Research System, 10th Edition, vol. 2. (Saunders College Publishing)


 

Index

NAME
SYNOPSIS
DESCRIPTION
OPTIONS
COMMANDS
Background Tasks
Subshells
Line continuation
Quoting
Grouping
Input and output
Pipes
Commands as Arguments
CONTROL STRUCTURES
If-else Statements
While and For Loops
Switch
Logical Operators
PATTERN MATCHING
LISTS AND VARIABLES
List Concatenation
Free Carets
Variables
Local Variables
Variable Subscripts
Flat Lists
Backquote Substitution
SPECIAL VARIABLES
FUNCTIONS
INTERRUPTS AND SIGNALS
BUILTIN COMMANDS
GRAMMAR
FILES
CREDITS
BUGS
INCOMPATIBILITIES
SEE ALSO

This document was created by man2html, using the manual pages.
Time: 06:48:06 GMT, December 12, 2024