Rc aims to improve on the default UNIX shell and it introduces a cleaner syntax for writing scripts. It is also built to be as simple to understand as possible. This makes rc a good program for someone that wants to learn how a shell works.
What Is Plan 9?
In a basic sense, Plan 9 is the successor to the UNIX operating system. It was developed by Bell Labs as an experimental project from the early 1980s up until 2015. Since then, a group of volunteers have been maintaining the operating system and it still sees occasional updates to this day. Plan 9 aims to produce a simple and approachable system for both developers and users alike. It does this by implementing and improving on the basic UNIX concept. As such, Plan 9 carries a lot of similar commands to UNIX and other UNIX-likes such as cp, mv and rm. Further, it also adds a number of new utilities and features that are unique to the operating system. For example, sam is a powerful text editor that you can fully control using regular expressions. One of the most innovative features of Plan 9 is the rc shell. It is a command interpreter similar to Bash and it allows you to create scripts which you can then use to automate your machine. rc also aims to simplify how you create these scripts by removing all unnecessary and redundant features.
Installing the Rc Shell
Installing the rc shell in Linux is relatively straightforward. Save and exit the file, and restart the rc shell.
Using and Writing Basic Rc Scripts
With that done, you can now start scripting in rc. In order to access this shell, all you need to do is to run the following command: Doing that will change the prompt of your terminal to “%”. This indicates that you are now inside the rc shell. From here, you can still access all of your files as well as traditional Linux commands. For example, we’ll type ls to get a list of the current directory.
Variables and File Operators
One of the most common uses for a shell language is for storing arbitrary values as well as manipulating files. In this regard, rc shell behaves exactly like Bash.
Using UNIX Pipes in Rc Shell
Another common function in shell languages is the ability to create UNIX pipes. This is a special type of file operator where you can pass the output of one program to a different program’s input. Similar to the functions above, rc shell largely follows the traditional Bash syntax when creating UNIX pipes.
Chaining Commands in Rc Shell
Aside from using UNIX pipes, it is also possible to chain different commands in rc shell. This allows you to run programs in sequence without the need to type them in separate lines. Check out this command that allows you to run less after running echo: Further, rc also allows you to check a program’s status code before going to an adjacent command. This can be useful if your program depends on either the success or failure of other programs. To this end, rc provides two operators: &&” and ||. These are special characters which you can use to replace the semicolon symbol.
The && operator tells rc to chain and run programs as long as the command on the left side runs successfully. This is helpful in situations where you need the whole chain to run properly.The || operator tells rc to chain and run programs if the command on the left side fails to run. Unlike the previous operator, this is incredibly useful if you are trying to fix issues in your scripts and programs.
Knowing that, you can then use these operators to create commands that can queue and check programs. For example, we’ll instruct rc to only run touch if mkdir runs successfully:
Using Strings and Lists in Rc Shell
One of the biggest differences between Bash and the rc shell is the latter’s focus on simplifying the use of plain text. In traditional shell, text and strings are often hard to declare in functions and variables. This issue is partly due to the fact that there are two ways to declare a string in Bash. It is possible to enclose text in both single and double quotes to treat a word as a string. As such, rc aims to reduce this complexity by only using single quotes when declaring strings. Doing this allows you to use special and control characters without any issues. By using this command we’ll turn a bracket to a string without any escape characters: Further, rc also allows you to manipulate these string variables and directly compile them as a list. This can be especially useful if you want to create a script that stores variables in a simple array. To do this, all you need to do is to enclose a group of strings in parenthesis. As such, we’ll write the following to create a variable with 5 elements inside it: Knowing that, you can then use a variety of commands to pull data out of an array. For example, this command will pull the first three elements of an array:
Basic Flow Control in Rc Shell
Aside from being a simple shell, rc is also a full programming language. This means that it is capable of not only basic string manipulation but also of logical branching similar to Python. Rc allows you to easily create if-then statements, for loops as well as the ability to define complex functions. This can be incredibly helpful if you are just starting out with programming.
Using Conditional Statements
One of the most basic feature of programming languages is the ability to create conditional statements. These are structures which tell a program what to do depending on the circumstance that it is in. For example, you can tell a program to only run if it has a value of 1. You can write a conditional statement for rc in a single line:
The “condition” field is a list of commands that rc will attempt to run. The way it works is that any program that successfully runs itself provides a value of “true”.The “program” field, on the other hand, depends on the condition field’s “true” value to run any command that you give to it.
Unlike Bash, this approach allows you to use almost any type of program for a script’s conditional statement. For example, you can tell rc to only run this statement if a hello.world file exists in the current directory:
Creating Simple for Loops
Aside from writing conditional statements, it is also possible to use rc for creating repeating scripts through loops. A loop is a structure that allows you to repeat a particular command for set number of times. Similar to conditional statements, you can also write the syntax of a loop in a single line:
The “step” field can either be a variable or a program that rc will go through one by one. For example, writing a list here will tell rc to go through each element of the list.The “program” field is the line of code that you want rc to run every time it goes through the step field.
Creating Functions in Rc
Lastly, it is also possible to create a general function in rc. Similar to other programming languages, this is a structure that you can define to perform a custom task. For example, the following will create a function that uses both conditional statements and for loops: While this might seem complex, writing a function is incredibly simple. At its core, the basic syntax of an rc function looks something like this:
The “name” field tells rc the label of a particular function. This can be especially useful if you want to reuse the same function for a later command.The “function” field, on the other hand, contains all the variables and commands that you want it to run.
Image credits: Unsplash All screenshots by Ramces Red