Test if a variable exists, if it is an array and if it is a function.
The library adds functions to a Bash environment so your shell scripts can leverage this extra functionality. Though it is intended to be included with BPM, that is not strictly necessary because this has no dependencies.
Add to your bpm.ini
file the following dependency.
[dependencies]
is=*
Run bpm install
to add the library. Finally, use it in your scripts.
#!/usr/bin/env bash
. bpm
bpm::include is
Determine if a given environment variable exists and if it is an array.
- $1 - Name of environment variable.
Examples
var=(abc)
if is::array var; then
echo "This is an array"
echo "Be careful to use the variable's name in the function call."
fi
declare -A associativeArray
associativeArray[test]=value
if is::array associativeArray; then
echo "This is also an array"
fi
right=wrong
wrong=()
if is::array $right; then
echo "This also says it is an array, which is incorrect."
echo 'The correct call is `is::array right` without the $.'
fi
Returns true (0) if the named variable exists and if it is an array. Returns false (1) otherwise.
Determine if a given environment variable exists and if it is an associative array.
- $1 - Name of environment variable.
Examples
declare -A associativeArray
associativeArray[test]=value
if is::associativeArray associativeArray; then
echo "This is also an array"
fi
var=(abc)
if is::associativeArray var; then
echo "This is false - $var is not an associative array"
fi
Returns true (0) if the named variable exists and if it is an associative array. Returns false (1) otherwise.
Determine if the given name is a defined function.
- $1 - Function name to check.
Examples
moo () {
echo "This is a function"
}
if is::function moo; then
echo "moo is a defined function"
fi
Returns true (0) if the name is a function, false (1) otherwise.
Determine if a variable is assigned, even if it is assigned an empty value.
- $1 - Variable name to check.
Examples
unset abc
if ! is::set abc; then
echo "The variable is not set"
fi
abc=""
if is::set abc; then
echo "This is true, the variable is set"
fi
Returns true (0) if the variable is set, false (1) if the variable is unset.
This project is placed under an MIT License.