cl-script — Using Common Lisp as Script Interpreter

In the Unix family of operating systems, there is the convention, that for an interpreted (or just in time compiled) language foo there is an interpreter foo that can be used in a hash-bang line in a script in the following way:

#!/usr/bin/foo

Or, if foo needs to be searched in $PATH:

#!/usr/bin/env foo

By the way, people, it's called a hash-bang line for obvious reasons, not a shebang as some people with hearing problems seem to have understood recently. It's a pet peeve of mine. But I digress.

When the kernel starts a program, it sees the magic bytes #! and instead of loading the program as an executable it loads the executable given after the #! and invokes that and passes the script path as the first parameter.

See this as an indirection. It's very cheap and surprisingly powerful (like so many features from the Unix heritage).

Now, I have been of the opinion for some time, that some Common Lisp implementations would be ideal scripting languages. Some (like sbcl) can indeed easily be used as script interpreters already: sbcl has a --script parameter, that can be used in the following fashion:

#!/usr/bin/env sbcl --script

Unfortunately not all interesting Common Lisp implementations that are still maintained for Unix (I count approximately 7-10) provide such a facility.

This is a pity, since such a facility would also provide an easy way to deploy applications without having to dump these very large images.

Roswell provides wrappers to use a Common Lisp as script interpreter, see Roswell as a Scripting Environment. Unfortunately it only "supports sbcl, ccl, clisp and ecl as its supported lisp implementations", so only four of the currently eight implementations I run on my workstation: sbcl, ccl, abcl, ecl, clasp, clisp, cmucl and mkcl.

I am very much interested to port as many infrastructure as is possible to all those lisps, so I need a wrapper of my own. It's called cl-script and a current spike can be found at codeberg.

You would typically use it like this:

#!/usr/bin/env cl-script

when cl-script is in path.

An it supports all eight Common Lisps listed above: sbcl, ccl, abcl, ecl, clasp, clisp, cmucl.

The default implemention cl-script invokes is sbcl (because it's the best supported implementation theses day in my opinion and because it is the fastest to start up), but one can switch the implementation to be used by setting the environment variable COMMON_LISP:

$ COMMON_LISP=abcl
$ examples/hello.cl  # <= now uses abcl