Module bash_builtins::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
Enums
Traits
DynamicVariable
provides the implementation to create dynamic
variables.Functions
name
.name
.name
to an instance of
DynamicVariable
.name
.name
.name
.name
.name
.