Skip to content

Script Writers

rb.scripts is sugar over rb.file() for executable scripts. Every helper does the same three things:

  1. Prepends #!/usr/bin/env <interpreter> (or the literal interpreter path if it begins with /).
  2. Writes the body via the standard deferred WriteFile op.
  3. Defers a Chmod op that sets mode 0755.

Both ops participate in rb plan and rb apply like any other Rootbeer side effect.

lua
local rb = require("rootbeer")

rb.scripts.bash("~/.local/bin/hello", [[
  echo "hello $1"
]])

rb.scripts.python("~/.local/bin/greet", [[
  import sys
  print(f"hello, {sys.argv[1] if len(sys.argv) > 1 else 'world'}")
]])

What these writers do — and don't

Script writers do not validate the body, install the interpreter, or manage PATH. The interpreter is referenced by name and must already be resolvable on the target machine when the script runs — linting and toolchain isolation are out of scope. A trailing newline is added if the body doesn't already end in one, so the resulting file is always well-formed.

Named helpers

Each helper is rb.scripts.<lang>(path, body):

HelperShebang
rb.scripts.bash#!/usr/bin/env bash
rb.scripts.sh#!/usr/bin/env sh
rb.scripts.zsh#!/usr/bin/env zsh
rb.scripts.fish#!/usr/bin/env fish
rb.scripts.python#!/usr/bin/env python3
rb.scripts.node#!/usr/bin/env node
rb.scripts.lua#!/usr/bin/env lua
rb.scripts.nu#!/usr/bin/env nu
rb.scripts.ruby#!/usr/bin/env ruby
rb.scripts.perl#!/usr/bin/env perl

Custom interpreters

Use rb.scripts.script(interpreter, path, body) for languages without a named helper, or to pin a specific interpreter path:

lua
rb.scripts.script("awk", "~/.local/bin/sum", [[
  { total += $1 } END { print total }
]])

rb.scripts.script("/opt/homebrew/bin/python3.12", "~/.local/bin/pinned", [[
  import sys; print(sys.version)
]])

A bare command ("awk") becomes #!/usr/bin/env awk. An absolute path (/opt/homebrew/bin/python3.12) is used verbatim as the shebang.

API

rootbeer.scripts.bash(path, body)

Writes an executable Bash script (#!/usr/bin/env bash). Defers a WriteFile op for the body and a Chmod op (mode 0755).

Parameters

pathstring
Destination file path.
bodystring
Script body.

rootbeer.scripts.fish(path, body)

Writes an executable Fish script (#!/usr/bin/env fish).

Parameters

pathstring
Destination file path.
bodystring
Script body.

rootbeer.scripts.lua(path, body)

Writes an executable Lua script (#!/usr/bin/env lua).

Parameters

pathstring
Destination file path.
bodystring
Script body.

rootbeer.scripts.node(path, body)

Writes an executable Node.js script (#!/usr/bin/env node).

Parameters

pathstring
Destination file path.
bodystring
Script body.

rootbeer.scripts.nu(path, body)

Writes an executable Nushell script (#!/usr/bin/env nu).

Parameters

pathstring
Destination file path.
bodystring
Script body.

rootbeer.scripts.perl(path, body)

Writes an executable Perl script (#!/usr/bin/env perl).

Parameters

pathstring
Destination file path.
bodystring
Script body.

rootbeer.scripts.python(path, body)

Writes an executable Python 3 script (#!/usr/bin/env python3).

Parameters

pathstring
Destination file path.
bodystring
Script body.

rootbeer.scripts.ruby(path, body)

Writes an executable Ruby script (#!/usr/bin/env ruby).

Parameters

pathstring
Destination file path.
bodystring
Script body.

rootbeer.scripts.script(interpreter, path, body)

Generic script writer with a custom interpreter shebang. Use a bare command ("bash") which becomes #!/usr/bin/env bash, or an absolute path ("/bin/sh") which is used verbatim. Prefer the named helpers (bash, python, …) for common languages.

Parameters

interpreterstring
Bare command name or absolute interpreter path.
pathstring
Destination file path (~ is expanded).
bodystring
Script body. A trailing newline is added if missing.

rootbeer.scripts.sh(path, body)

Writes an executable POSIX sh script (#!/usr/bin/env sh).

Parameters

pathstring
Destination file path.
bodystring
Script body.

rootbeer.scripts.zsh(path, body)

Writes an executable Zsh script (#!/usr/bin/env zsh).

Parameters

pathstring
Destination file path.
bodystring
Script body.