How to write Guile scripts
Guile is a Scheme implementation that comes with many GNU/Linux distributions. Some of Guile's attributes make it very practical to use on UNIX-like systems:
- Ability to write quick scripts with low start-up time.
- Relatively low-level access to system calls, if needed.
- Supports interaction with C code.
This document outlines the basic interaction between Guile and the UNIX environment.
Making it executable
Mark your file as executable (
chmod +x FILENAME) and begin your
Guile script with these three lines:
#!/usr/bin/guile \ -e main -s !#
How parameters are passed
When executing such a Guile file, UNIX will run the command
/usr/bin/guile '\' FILENAME ARG1 ARG2 ... ARGN
Guile will recognize the backslash parameter
\ ("meta switch") and
behave as if it had been called with the additional parameters on the
second line of the file.
/usr/bin/guile -e main -s FILENAME ARG1 ARG2 ... ARGN
The interpreter will now load the script in
FILENAME, ignore the
first three lines as multiline comment, and call the procedure named
main with the argument list (including the script name)
(list FILENAME ARG1 ARG2 ... ARGN), as instructed by the
parameter. (You will need to have the procedure
Quick summary of the options being used here:
-s FILE: load the given file
-e NAME: execute
(NAME ARG1 ARG2 ... ARGN)
This is a simplified implementation of the
#!/usr/bin/guile \ -e main -s !# (define (main args) (display (string-join (cdr args) " ")) (newline))
Note that we skip the first element of
args, which contains the
To execute it, run:
$ chmod +x echo.scm $ ./echo.scm hello world hello world
This may spit some compiler notes on first execution.
Comparison with other languages
Normally, UNIX-enabled scripting languages only require one hashbang
#!) line to specify the interpreter to be used. Many languages
deal with this by making the hash (
#) character the character which
starts a one-line comment.
In Guile, one-line comments are started with a semicolon (
denotes the start of a multiline comment, which is closed with
Therefore, scripts starting with a hashbang section as above will
still be valid Guile scripts.