Bash.AST.Word (Bash v0.3.0)

Copy Markdown View Source

Word: expandable text that may contain variables, command substitutions, etc.

A word is composed of parts that are either literals or expansions. Quoting affects how expansions are performed.

Examples

# hello (literal)
%Word{
  parts: [{:literal, "hello"}],
  quoted: :none
}

# $USER (variable)
%Word{
  parts: [{:variable, %Variable{name: "USER"}}],
  quoted: :none
}

# "hello $USER" (double-quoted with expansion)
%Word{
  parts: [
    {:literal, "hello "},
    {:variable, %Variable{name: "USER"}}
  ],
  quoted: :double
}

# 'hello $USER' (single-quoted, no expansion)
%Word{
  parts: [{:literal, "hello $USER"}],
  quoted: :single
}

# $(echo test) (command substitution)
%Word{
  parts: [
    {:command_subst, [%Command{name: "echo", args: ["test"]}]}
  ],
  quoted: :none
}

# $((1 + 2)) (arithmetic expansion)
%Word{
  parts: [{:arith_expand, "1 + 2"}],
  quoted: :none
}

# *.txt (glob pattern)
%Word{
  parts: [{:glob, "*.txt"}],
  quoted: :none
}

Summary

Types

part()

@type part() ::
  {:literal, String.t()}
  | {:variable, Variable.t()}
  | {:command_subst, [Statement.t()]}
  | {:process_subst_in, [Statement.t()]}
  | {:process_subst_out, [Statement.t()]}
  | {:arith_expand, String.t()}
  | {:glob, String.t()}
  | {:brace_expand, BraceExpand.t()}

quote_type()

@type quote_type() :: :none | :single | :double

t()

@type t() :: %Bash.AST.Word{
  meta: Bash.AST.Meta.t(),
  parts: [part()],
  quoted: quote_type()
}