Bash.Sigil (Bash v0.6.0)

Copy Markdown View Source

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 ("hello or 'hello)
  • Unclosed command groups ({ echo hello)
  • Missing control flow terminators (if without fi, do without done)

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

sigil_BASH(arg, modifiers)

(macro)

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 string
  • E: Execute and return stderr as string
  • O: Execute and return combined output as string

Session option modifiers (lowercase, can combine)

  • e: Enable errexit (exit on error)
  • v: Enable verbose mode
  • p: Enable pipefail
  • u: 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"