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.
- Variable
Error - An error from a shell variable operation, like
set
orbind
.
Traits§
- Dynamic
Variable - 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 ofDynamicVariable
. - 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
.