Bash.Variable (Bash v0.6.0)

Copy Markdown View Source

Bash variable with attributes and value. Supports scalar values, indexed arrays, and associative arrays.

Working with Sessions

You can retrieve variables directly from a session:

Variable.get(session, "myvar")           # Get scalar variable
Variable.get(session, "myarray", 0)      # Get array element at index 0
Variable.get(session, "myassoc", "key")  # Get associative array element

Or work with Variable structs directly:

var = Variable.new("hello")
Variable.get(var, nil)  # => "hello"

Summary

Functions

Get all keys for ${!arr[@]} expansion

Get all values for ${arr[@]} expansion

Returns true if the variable is an array (indexed or associative).

Returns true if the variable is an associative array.

Get length for ${#arr[@]} or ${#var}

Returns true if the variable is a nameref.

Get the nameref target variable name, or nil if not a nameref.

Create scalar variable

Create associative array

Create indexed array

Create a nameref variable that references another variable by name

Create a declared-but-unset variable (for local x / declare x without value)

Returns true if the variable is marked readonly.

Set value at index, returns new Variable

Returns true if the variable was declared but never assigned a value.

Types

array_type()

@type array_type() :: :indexed | :associative | nil

attributes()

@type attributes() :: %{
  readonly: boolean(),
  export: boolean(),
  integer: boolean(),
  array_type: array_type(),
  nameref: String.t() | nil
}

t()

@type t() :: %Bash.Variable{attributes: attributes(), value: value()}

value()

@type value() ::
  String.t()
  | %{required(integer()) => String.t()}
  | %{required(String.t()) => String.t()}

Functions

all_keys(arg1)

Get all keys for ${!arr[@]} expansion

all_values(variable)

Get all values for ${arr[@]} expansion

array?(arg1)

Returns true if the variable is an array (indexed or associative).

get(session, var_name, index_or_key)

is_associative_array?(arg1)

Returns true if the variable is an associative array.

length(variable)

Get length for ${#arr[@]} or ${#var}

nameref?(arg1)

Returns true if the variable is a nameref.

nameref_target(arg1)

Get the nameref target variable name, or nil if not a nameref.

new(value \\ "")

Create scalar variable

new_associative_array(values \\ %{})

Create associative array

new_indexed_array(values \\ %{})

Create indexed array

new_nameref(target_name)

Create a nameref variable that references another variable by name

new_unset()

Create a declared-but-unset variable (for local x / declare x without value)

readonly?(arg1)

Returns true if the variable is marked readonly.

set(var, value, idx)

Set value at index, returns new Variable

unset?(arg1)

Returns true if the variable was declared but never assigned a value.