Getting started with the rc shell

MJ Ray

15th August 2003

This is a work in progress. Non-linear pipes aren’t mentioned, for example.

Contents

1 Why Another Shell?
2 Where It Came From
3 Back To Grammar School
4 Command and Control
5 Summary

1 Why Another Shell?

Some of us are perfectly happy with bash as our shell. So be it. I’m not going to try to persuade anyone else to change the shell they use. I’m just going to state the benefits of this interesting little shell.

For me, the major problem with bash is the complexity of its syntax. It is a very powerful tool, but I don’t use that power every day. Whenever I need to use an advanced feature, I normally have to look at the man page for it. The manual page is not a small affair, with over five and a half thousand lines. If you only know what you are trying to accomplish, not the technical name for it, it can be a tricky affair. Navigating the manual page becomes a skill in itself

The rc shell is somewhat simpler. It can’t quite do everything that bash can, but it is closer to the normal unix philosophy of “pick one thing and do it well”.

2 Where It Came From

Following the unix philosophy is particularly ironic. The manual here for rc says ”It is based on the AT&T Plan 9 shell of the same name.” Plan 9[1] is a radically different operating system to the modern unixes that most users run rc on today, with everything being distributed in nature. It’s still available today, under a restricted licence.

Many unix users like the attitude of some of the tools and some of them have been ported to unix-like systems where the licence permits. Others have been ”cloned” by enthusiastic unix hackers and developed further in their new home. I became interested in rc because of a particularly good text editor named wily, which was inspired by an editor called acme on Plan 9.

3 Back To Grammar School

So, enough waffle, on with the basics. The rc man page contains the grammar for the shell as a definitive reference if you can read grammars. The rest of us mortals can read the wording before it.

Basic ideas such as commands are done just as before, simply by typing the command. They can be backgrounded by putting an & at the end of it, similar to other shells.

Variable assignment is similar to Bourne shells (the main family of shells, including bash), too. Simply, it is VARNAME = value. Here we meet our first differences. There is no export command: all variables are environment variables exported to subprocesses by default. So, instead of “export TEXINPUTS=foo”, it is simply “TEXINPUTS=foo”. Also, there is no “unset” command, with variables being deleted automatically when set to the null list, written ().

Command substitution is done with backquotes, but in a different way to Bourne shells. Technically, the backquote is a unary operator which executes the following word as a command and puts the output in its place, so putting `pwd on the command line is replaced by the current directory. If you want to run a multi-word command, you can put braces {} around the command, eg `{ls -t}.

When you try to do something like cat `{mhpath}/407, you will hit the first problem. The carriage return included in the output of mhpath is included and forms a space between the directory and the string you typed. No problem! Just use the caret (^) to close up that space cat `{mhpath}^/407.

Another unary operator is the at sign (@), which runs the following command in a subshell where it does not affect the state of the current shell (variables, directory, etc). That is to say, running @{cd test && ls -lh} leaves the current shell in its current directory after the command completes, not in the test directory.

A potential gotcha is in the simpler quoting rules. Basically, rc only knows the single quotes make anything that appears between them be passed through the shell as a single argument without processing. If you want to include a single quote, put two next to each other and they’ll be passed through as one. rc treats double quotes like any other character and does no special treatment - that can catch you out the first few times.

Finally, stream redirection is similar to in Bourne shells. Standard input is stream 0, output is stream 1 and error is stream 2. Redirection is done with symbols like >[2] file to redirect standard error to file. Stream duplication (2>&1 in Bourne shell) is done with the symbols >[2=1] to redirect standard error to standard output.

4 Command and Control

There are a lot fewer built-in commands than in a typical Bourne shell. This isn’t really a serious problem, as some of the commands are duplicated by external programs in the shell utilities normally installed on most systems. The only command which has been included in the shell for performance reasons is the venerable echo which is called very frequently in some loops.

This doesn’t mean that it’s a restrictive or harsh place to work in. One feature that was news to me, even though it was in my previous shell, is the cdpath variable. If you set this to a list of directories, rc will look along it for any directories that you try to change into. Make sure you include “.” at the start, though, so as not to confuse yourself.

The shell programming language has some similarities to both Bourne and C shell, but is unlike either. Serious scripters will probably want to take a look at the facilities offered and the manual page is a short, fairly painless read for that sort of person, especially if you are used to wading through a long shell manual page, as I was.

The one deficiency that I noticed is that there is no job control: you cannot just hit ^Z to suspend a program, do something else and flip back to it. This isn’t really a major problem, as you can always open another shell, but it is a surprise. Background jobs (ones started with & after the command) have their process IDs stored in the apids variable, so ps $apids shows you the jobs started by this shell.

5 Summary

I hope that I have given you a flavour of the rc shell and encouraged you to take a quick look at it. It is quite pleasing to work in and takes only a few hours to completely master. (The experienced hacker can probably do it even faster, because of the completely regular small grammar.)

Copyright

Copyright 2002 MJ Ray markj at luminas.co.uk http://mjr.towers.org.uk/

This work may be reproduced in any form, as long as the above copyright notice is preserved, its author is clearly attributed, all modifications are clearly marked and all recipients of a copy or derived work are granted these same rights.

References

[1]   Plan 9 from Bell Labs