Module variables

Source
Expand description

This module contains functions to get, set, or unset shell variables.

Use set and unset to modify shell variables.

Use the find functions to access the value contained in existing shell variables. find_raw provides access to the raw pointer owned by bash, and both find and find_as_string provides a safe interface to such value.

Use array_set and array_get to access the elements in an indexed array.

Use assoc_get and assoc_get to access the elements in an associative array.

§Example

The following example uses the shell variable $SOMENAME_LIMIT to set the configuration value for the builtin. If it is not present, or its value is not a valid usize, it uses a default value

use bash_builtins::variables;

const DEFAULT_LIMIT: usize = 1024;

const LIMIT_VAR_NAME: &str = "SOMENAME_LIMIT";

fn get_limit() -> usize {
    variables::find_as_string(LIMIT_VAR_NAME)
        .as_ref()
        .and_then(|v| v.to_str().ok())
        .and_then(|v| v.parse().ok())
        .unwrap_or(DEFAULT_LIMIT)
}

§Dynamic Variables

Dynamic variables are shell variables that use custom functions each time they are accessed (like $SECONDS or $RANDOM).

Use bind to create a dynamic variable with any type implementing DynamicVariable.

Structs§

RawVariable
Raw reference to a shell variable.

Enums§

Variable
Contains the value of a shell variable.
VariableError
An error from a shell variable operation, like set or bind.

Traits§

DynamicVariable
The DynamicVariable provides the implementation to create dynamic variables.

Functions§

array_get
Returns a copy of the value corresponding to an element in the array.
array_set
Change an element of the array contained in the shell variable referenced by name.
assoc_get
Returns a copy of the value corresponding to a key in an associative array.
assoc_set
Change an element of the associative array contained in the shell variable referenced by name.
bind
Bind the shell variable referenced by name to an instance of DynamicVariable.
find
Returns a copy of the value of the shell variable referenced by name.
find_as_string
Returns a string with the value of the shell variable name.
find_raw
Returns a reference to the address of the shell variable referenced by name.
set
Sets the value of the shell variable referenced by name.
unset
Unset the shell variable referenced by name.