Sigil implementation for ~BASH.
This module provides the ~BASH sigil that parses Bash scripts at compile time
and returns a Script struct for execution. Supports Elixir #{} interpolation.
Validation
The sigil performs structural validation on the parsed AST, detecting common errors:
- Unclosed quotes (
"helloor'hello) - Unclosed command groups (
{ echo hello) - Missing control flow terminators (
ifwithoutfi,dowithoutdone)
Errors include ShellCheck-compatible codes and helpful hints.
Examples
# Valid scripts parse successfully:
~BASH"echo hello"
~BASH"if true; then echo hi; fi"
# Invalid scripts raise SyntaxError:
~BASH"{ echo hello"
# => ** (Bash.SyntaxError) [SC1056] Bash syntax error at line 1, column 0:
# { echo hello
# ^
# hint: '{' starts a command group - missing '}'
Summary
Functions
The ~BASH sigil for parsing Bash scripts into a Script AST struct.
Functions
The ~BASH sigil for parsing Bash scripts into a Script AST struct.
Returns a Script struct that can be serialized back to bash strings.
Modifiers
Modifiers can be combined. Lowercase modifiers set session options, uppercase modifiers control output.
Output modifiers (uppercase, mutually exclusive)
- No output modifier: Returns
%Bash.Script{}struct (default) S: Execute and return stdout as stringE: Execute and return stderr as stringO: Execute and return combined output as string
Session option modifiers (lowercase, can combine)
e: Enable errexit (exit on error)v: Enable verbose modep: Enable pipefailu: Enable nounset (error on undefined variables)
Examples
# Single command returns a Script:
iex> ~BASH"echo hello world"
%Bash.Script{statements: [%Bash.AST.Command{...}]}
# Serialize back to string:
iex> script = ~BASH"echo hello"
iex> to_string(script)
"echo hello"
# Execute and get stdout:
iex> ~BASH"echo hello"S
"hello\n"
# Execute and get stderr:
iex> ~BASH"echo error >&2"E
"error\n"
# Execute and get combined output:
iex> ~BASH"echo out; echo err >&2"O
"out\nerr\n"
# Execute with errexit and return stdout:
iex> ~BASH"echo hello"eS
"hello\n"
# Execute with pipefail and errexit:
iex> ~BASH"false | echo hi"epS
"hi\n"