Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Built-in Functions

Relux provides built-in functions (BIFs) that are always available without imports. BIFs are divided into two categories based on their purity — whether they require a shell context to operate.

Purity

  • Pure BIFs do not interact with any shell. They can be called from pure functions, condition markers, overlay expressions, and regular shell blocks.
  • Impure BIFs require a shell context (they send input or match output). They can only be called inside shell blocks and regular (non-pure) functions.

“Pure” here means shell-independent, not side-effect-free — pure BIFs may still perform I/O (e.g. sleep, log, which).

Pure BIFs

String

FunctionSignatureReturnsDescription
trimtrim(s)stringRemove leading and trailing whitespace from s.
upperupper(s)stringConvert s to uppercase.
lowerlower(s)stringConvert s to lowercase.
replacereplace(s, from, to)stringReplace all occurrences of from with to in s.
splitsplit(s, sep, index)stringSplit s by sep and return the part at index (0-based). Returns "" if the index is out of bounds. Errors if index is not a valid integer.
lenlen(s)stringReturn the byte length of s as a decimal string.
defaultdefault(a, b)stringReturn a if it is non-empty, otherwise return b.

Generators

FunctionSignatureReturnsDescription
uuiduuid()stringGenerate a random UUID v4 (e.g. "550e8400-e29b-41d4-a716-446655440000").
randrand(n)stringGenerate a random alphanumeric string of length n. Errors if n is not a valid integer.
randrand(n, mode)stringGenerate a random string of length n using the given charset mode. Modes: alpha, num, alphanum, hex, oct, bin. Errors if mode is unknown or n is not a valid integer.

System

FunctionSignatureReturnsDescription
available_portavailable_port()stringBind to an ephemeral TCP port on 127.0.0.1 and return the port number. The port is released after the call, so it may be reused — call this close to where the port is needed.
whichwhich(name)stringSearch PATH for an executable named name. Returns the absolute path to the first match, or "" if not found. Checks that the file has an executable permission bit set. If name contains a path separator, checks that path directly instead of searching PATH.
sleepsleep(duration)""Pause execution for duration. Accepts humantime format: 500ms, 2s, 1m30s, etc. Errors if the duration is invalid.

Logging

FunctionSignatureReturnsDescription
loglog(message)stringEmit message to the event log and HTML report. Returns message.
annotateannotate(text)stringEmit text as a progress annotation (visible in verbose output). Returns text.

Impure BIFs

Shell matching

FunctionSignatureReturnsDescription
match_promptmatch_prompt()stringMatch the shell prompt configured in Relux.toml. Advances the output cursor past the prompt.
match_okmatch_ok()stringMatch the shell prompt, send echo $?, match 0, and match the prompt again. Verifies the previous command exited with status 0.
match_not_okmatch_not_ok()stringMatch the shell prompt, verify the previous command exited with a non-zero status, and match the prompt again. The inverse of match_ok().
match_not_okmatch_not_ok(code)stringMatch the shell prompt, verify the previous command exited with a specific non-zero status code, and match the prompt again. Like match_exit_code(code) but also asserts the code is non-zero.
match_exit_codematch_exit_code(code)stringSend echo $?, match code, and match the prompt. Verifies the previous command exited with the given status. code is passed as a bare literal (e.g. match_exit_code(1)).

Control characters

FunctionSignatureReturnsDescription
ctrl_cctrl_c()""Send ETX (0x03) — interrupt the current process.
ctrl_dctrl_d()""Send EOT (0x04) — signal end of input.
ctrl_zctrl_z()""Send SUB (0x1A) — suspend the current process.
ctrl_lctrl_l()""Send FF (0x0C) — clear the terminal screen.
ctrl_backslashctrl_backslash()""Send FS (0x1C) — send SIGQUIT to the current process.