Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

206 lines (152 sloc) 4.338 kB

Pallet Reference - Script

Pallet scripts are written using an embedding of shell script in Clojure, and allows for abstraction of operating system variations.

At it’s simplest, the script macro takes one or more forms and converts them to shell script.

  '[pallet.stevedore :as stevedore])



There is an obvious mismatch between Clojure’s immutable data approach, and shell script’s mutable variables.


For accessing the value of a script variable, stevedore uses `deref` or `@`, which are also used for command substitution.

 (deref TMPDIR))

If the variable isn’t set, a default can be supplied.



Values can be defined with `set!`.

 (set! x 1))

Command Substitution

Similar to variable substitution, command substitution uses `deref`.

 (set! R @(cat "/etc/redhat-release")))


Shell script `for` loops are mapped to clojure`s `doseq`.

 (doseq [x ["a" "b" "c"]]
   (println @x)))


Calling a function follows clojure’s normal syntax.

 (foo x y))

Defining a function follows a simplified version of clojure’s `defn`, and does not allow for destructuring. Arguments are named and automatically mapped to the shell function’s numbered arguments.

 (script (defn foo [x y] (bar x))))


Array literals are generated from clojure’s `vector` notation.

 [1 "2" (quoted 3) :four])

Array access uses `aget` and `aset`.

 (aget foo 1))
 (aset foo 1 :foo))


One advantage of embedding script in clojure is that we can escape back to clojure to substitute in values.

(let [tmp "/tmp"]
    (ls ~tmp)))


We can also unquote arbitrary clojure expressions.

 (ls ~(str "/" "tmp")))


Sometimes we want to add the contents of a collection into a script, without demarking the boundaries of the collection.

(let [files ["a" "b" "c"]]
    (ls ~@files)))

Script functions

Script functions are defined with `pallet.script/defscript` and provide multimethods for shell script generation. In pallet this is used to abstract away script differences between operating systems.

  '[pallet.stevedore :as stevedore]
  '[pallet.script :as script])

(script/defscript ls [& args])
(stevedore/defimpl ls :default [& args]
  (ls ~@args))
(stevedore/defimpl ls [:windows] [& args]
  (dir ~@args))

(script/with-template [:windows]
    (ls a b c)))

Scripting Guide

An excellent reference on shell scripting is available at

Jump to Line
Something went wrong with that request. Please try again.