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§

Enums§

Traits§

  • The DynamicVariable provides the implementation to create dynamic variables.

Functions§

  • Returns a copy of the value corresponding to an element in the array.
  • Change an element of the array contained in the shell variable referenced by name.
  • Returns a copy of the value corresponding to a key in an associative array.
  • Change an element of the associative array contained in the shell variable referenced by name.
  • Bind the shell variable referenced by name to an instance of DynamicVariable.
  • Returns a copy of the value of the shell variable referenced by name.
  • Returns a string with the value of the shell variable name.
  • Returns a reference to the address of the shell variable referenced by name.
  • Return a copy of the last command’s exit status.
  • Sets the value of the shell variable referenced by name.
  • Unset the shell variable referenced by name.