Shell Expansion¶
Shell expansion replaces environment variable references and command substitutions with their values at parse time.
Syntax¶
| Pattern | Description | Example |
|---|---|---|
$VAR |
Environment variable | $USER |
${VAR} |
Environment variable (braced) | ${HOME} |
$(command) |
Command substitution | $(date +%Y-%m-%d) |
\$ |
Literal dollar sign | \$100 writes $100 |
Environment Variables¶
After parsing:
parsed = PM.parse('info.md')
parsed.content
#=> "Current user: dewayne\nHome directory: /Users/dewayne\n"
Only UPPERCASE variable names are expanded. Lowercase names like $foo are left as-is.
Missing environment variables are replaced with an empty string.
Command Substitution¶
Commands are executed via the shell and replaced with their stdout (trailing newline stripped).
Nested Commands¶
Nested parentheses are handled correctly:
Command output is inert¶
Expansion is a single left-to-right pass. Whatever a command prints is spliced into the content verbatim and not scanned again:
When ERB is enabled for the prompt, any <% in command output or in an environment
variable's value, or one formed where a non-empty expansion meets the text beside it, is escaped
to <%% so it cannot open a tag in the later ERB pass. The rendered result shows the
literal <%. An expansion written inside an ERB tag is part of that Ruby code, so the
escaping does not make it safe; see Security.
Standard error¶
A command's stderr is not part of the content. On success it is forwarded to the
process's $stderr. On failure it is included in the error.
Failed Commands¶
A command that exits with a non-zero status raises PM::ShellCommandError:
PM.parse_string("Output: $(exit 1)")
#=> PM::ShellCommandError: Shell command failed (exit 1): exit 1
The error exposes #command, #status (a Process::Status), and #stderr.
Commands That Cannot Run¶
A command that cannot be started at all, such as a mistyped name, raises
PM::ShellExpansionError. The underlying SystemCallError (for example
Errno::ENOENT) is available as #cause:
PM.parse_string("Output: $(no_such_cmd)")
#=> PM::ShellExpansionError: Shell command could not be run (No such file or directory - no_such_cmd): $(no_such_cmd)
An empty or blank $() raises PM::ShellExpansionError with the message
Empty shell command: $().
Unmatched $(¶
An opening $( with no closing parenthesis raises PM::ShellExpansionError
(the superclass of PM::ShellCommandError):
PM.expand_shell("Total: $(echo")
#=> PM::ShellExpansionError: Unmatched $( in shell expansion: $(echo
Escaping¶
Write \$ to produce a literal $:
Fenced Code Blocks¶
Lines inside a fenced code block (``` or ~~~, with an optional language tag) are copied through untouched, so a prompt can show shell syntax to the model without running it:
Only the last $HOME is expanded. A fence closes at a line that starts with at least
as many of the same fence characters.
Direct Access¶
Shell expansion is available as a standalone method:
PM.expand_shell("Hello $USER from $(hostname)")
#=> "Hello dewayne from venus"
PM.expand_shell(string, erb_safe: true) # also escapes <% in command output and env var values
Disabling Shell Expansion¶
Set shell: false in the file's YAML metadata:
Or disable globally:
Per-file metadata overrides the global setting unless the configuration is locked
with PM.config.lock!. See Configuration.
Interaction with ERB¶
Shell expansion happens at parse time (step 3 of the pipeline). ERB rendering happens later when to_s is called (step 4). This means shell references in the raw template are expanded before ERB sees the content.
If you need dynamic shell output at render time, use a custom directive instead: