Linux Fu: The Shell Forth Programmers Will Love

One particular of the most impressive options of Unix and Linux is that making use of regular command line applications, almost everything is a stream of bytes. Granted, modern day software program has blurred this a bit, but at the command line, almost everything is textual content with sure free conventions about what separates fields and documents. This lets you do things like get a directory listing, sort it, get rid of the duplicates, and review it to an additional directory listing. But what if the shell comprehended extra facts kinds other than streams? You could argue it would make some items superior and some factors even worse, but you never have to guess, you can install cosh, a shell that presents resources to deliver and do the job with structured facts styles.

The technique is written with Rust, so you will need to have Rust set up to compile it. For most distributions, that’s just a offer install (rust-all in Ubuntu-like distros, for case in point). As soon as you have it functioning, you will have a several new things to master as opposed to other shells you have made use of.


A fantastic way to get a brief flavor of the shell’s idiosyncracies is to distinction it with the common shell syntax. The Github page has quite a few fantastic examples:

  • Uncover documents matching a route, and lookup them for facts:

sh: locate . -iname ‘*exam*’ -print0 | xargs – grep knowledge
cosh: lsr [test m] grep [f<; [data m] grep] map

  • Come across the full dimensions of all documents in the current listing:

sh: ls | xargs stat -c %s | awk ‘s+=$1 Conclude print s’ –
cosh: ls [is-dir; not] grep [stat; size get] map sum

  • Get the second and third columns from each individual row of a CSV file:

sh: slash -d, -f2,3 examination-info/csv
cosh: test-details/csv f< [chomp; , split; (1 2) get] map

  • Sort files by modification time:

sh: ls -tr
cosh: ls [[stat; mtime get] 2 apply <=>] sortp

As you can see, sometimes instructions are a very little for a longer time, but presumably, there is fewer to keep in mind, and it is a little bit extra self-documenting.

But Why?

The crucial concept is that this shell understands various info types. In certain, it can offer with hash maps, sets, and lists. Basic products contain booleans, integers (32-bit or of arbitrary size), floats, and strings.

The input prompt is far more like a command prompt for a programming language. In simple fact, Forth programmers will recognize the RPN abilities:

/tmp/cosh$ 5 3 /
/tmp/cosh$ 5. 3 /

Storing into variables is very similar to Forth, far too, using ! and @ with the RPN-fashion notation. In point, it all seems like Forth from swap and drop to the way if controls conditionals. Having said that, the stack doesn’t exist involving strains. So the higher than illustrations do not depart the result on the stack.

The documentation on Github is superior, but there are a couple factors you are going to have to work out. For example, the string functionality ++ is documented, but the illustration employs the phrase append, which doesn’t seem to be to operate.

/tmp/cosh$ hacka day ++
/tmp/cosh$ hacka day append
working day

Instructions and Normal Expressions

Most shell commands exist in cosh, also, but not always as exterior equipment. Some have aliases, much too. For illustration, you can use mv, but you can also use rename. Every little thing is, of program, utilizing the RPN structure for arguments.

If you want an exterior command, you require to prefix it with a dollar indicator or, in an interactive shell, you can use a place if you prefer. For instance, if you operate ls, you’ll get the cosh edition of ls. If you operate $ls, that is the real ls command you expect.  If you place the exterior identify in braces, what is returned is a generator that lets you to stroll by way of the output.

What’s a shell without the need of normal expressions? With cosh, you have an “m” expression that tells you if you have a match or a “c” situation that returns captures from the expression. There are also “s” expressions for research and change. You can also include flags to let different options like situation insensitivity.

I observed the seize part confusing. You’d consider it would supply a record of things matched in parenthesis, but possibly it does not or I couldn’t discover the proper syntax to make it do so. For case in point:

/tmp/cosh$ title=al "identify=(.*)$" c
   : identify=al
/tmp/cosh$ identify=al,name=jim "title=([a-zA-z]+)/g" c
   : title=al
   1: identify=jim

The documentation demonstrates some examples of this that really don’t function specifically correct, far too. For instance, test this from the documentation:

/tmp/cosh$ asdf "(..)" c

To get the final result the doc exhibits, you will need the /g flag on the regular expression. Or else, only the initially match appears.


One particular big element of cosh is that it can parse json and XML. It can also write out information in that structure. We’d enjoy to see a suitable CSV parser, while which is a minimal less complicated to take care of straight with cosh primitives than an XML file.

For illustration, if you want the 3rd and 4th fields from a CSV file, you can go through it and use the split and get capabilities in a map:

/tmp/cosh$ take a look at.csv f< [chomp; , split; (3 4) get] map

Of course, that’s not going to handle things like quoted values, but that’s typically a problem in other simple shell scripts, too.

Working with json is easy. For example, if you want to find the fields that match a regular expression, you can do that:

file.json f< from-json keys [.4 m] grep&#13
v[gen (
0: asdf
1: qwer
2: tyui
3: zxcv
)]  # from the official examples


Will we start using cosh every day? Honestly, probably not. But it is interesting. We might keep it in our back pocket for writing scripts that need to process json or XML.

We like the Forth syntax, but not everyone will. We also like the data typing. But as a general-purpose shell, it seems to leave something to be desired.

Of course, what we really like is Linux gives you choices. If you like cosh, knock yourself out. Don’t like it? Pick another shell or — if you are feeling brave — write you own. The world is your oyster.

We couldn’t help but think of the database-like Crush shell while playing with cosh. Then here’s cat9, which is a strange shell, indeed. There are, too, some more mainstream alternative shells like zsh and fish.

Luis Robinson

Next Post

Modulex Is LEGO’s Long Lost Cousin

Wed Feb 8 , 2023
We really like LEGO in this article at Hackaday, but did you know that LEGO spun off a parallel product line produced for architectural designs called Modulex? [Peter Dibble] can take us on a deep dive as a result of the record of Modulex, commencing with Godtfred Kirk Christiansen needing a […]
Modulex Is LEGO’s Long Lost Cousin

You May Like