Central registry for Bash builtin commands and reserved words.
This module provides:
- List of reserved words (keywords)
- List of all standard bash builtins
- Map of implemented builtins to their modules
defbashmacro for implementing builtins with streaming I/O
Reference: https://cgit.git.savannah.gnu.org/plain/bash.git/tree/builtins?h=bash-5.3
Implementing Builtins with defbash
Use use Bash.Builtin and the defbash macro to implement builtins
with automatic streaming I/O through sinks:
defmodule Bash.Builtin.Echo do
use Bash.Builtin
defbash execute(args, state) do
text = Enum.join(args, " ")
puts(text)
:ok
end
endI/O Functions
Inside defbash, the following functions are available:
puts(message)- Write to stdout with newlinewrite(data)- Write raw data to stdouterror(message)- Write to stderr with newlinegets()- Read a line from stdinread(:all)- Read all stdin
State Updates
Use update_state/1 to request state changes:
defbash execute(args, state) do
update_state(working_dir: "/new/path", env: %{"FOO" => "bar"})
:ok
endReturn Values
:ok- Exit code 0{:ok, n}- Exit code n{:error, message}- Exit code 1, message to stderr
Summary
Functions
Sets up a module as a builtin implementation with streaming I/O.
Returns the list of all bash builtins (implemented and unimplemented).
Check if a name is a builtin (implemented or not).
Returns the map of implemented builtins to their module implementations.
Define a builtin's execute function with streaming I/O support.
Get the module for a builtin, if implemented.
Check if a builtin is implemented.
Returns the list of implemented builtin names.
Check if a name is a reserved word.
Returns the list of bash reserved words.
Functions
Sets up a module as a builtin implementation with streaming I/O.
Example
defmodule Bash.Builtin.MyBuiltin do
use Bash.Builtin
defbash execute(args, state) do
puts("Hello from builtin!")
:ok
end
end
Returns the list of all bash builtins (implemented and unimplemented).
Check if a name is a builtin (implemented or not).
Returns the map of implemented builtins to their module implementations.
Define a builtin's execute function with streaming I/O support.
The function receives:
args- List of string argumentsstate- The session state map (with sinks, variables, etc.)
Inside the body, use:
puts/1- Write line to stdoutwrite/1- Write raw to stdouterror/1- Write line to stderrgets/0- Read line from stdinread/1- Read from stdinupdate_state/1- Request state changes
Return values:
:ok- Success, exit code 0{:ok, n}- Success, exit code n{:error, message}- Failure, writes message to stderr
Get the module for a builtin, if implemented.
Check if a builtin is implemented.
Returns the list of implemented builtin names.
Check if a name is a reserved word.
Returns the list of bash reserved words.