Skip to main content

Checks

Every program and script will have to check a condition at some point. bashutils provides built-in methods for checking various bash-related things. All methods only return either 0 or 1 and won't access stdin, stdout or stderr.

You can use them like this:

# Boilerplate code
if is_command_builtin "echo"; then
echo "'echo' is built into bash"
fi
if ! is_command_defined "setup"; then
echo "ERROR: 'setup' is undefined"
fi

# Simple code
is_command_builtin "echo" && echo "'echo'" is built into bash"
! is_command_defined "setup" && echo "ERROR: 'setup' is undefined"

Command type

These checks check the type of the passed command and return 0 if it matches or 1 if it doesn't.

Checks for Usage
Aliases is_command_alias <str:command name>
Keywords is_command_keyword <str:command name>
Functions is_command_function <str:command name>
Builtins is_command_builtin <str:command name>
Files (Binaries & Scripts) is_command_file <str:command name>
Anything is_command_defined <str:command name>

Input type

These checks check the type of the specified input and return 0 if the type matches or 1 if it isn't.

Checks for Usage
bool is_input_bool <?:input>
byte is_input_byte <?:input>
int is_input_int <?:input>
char is_input_char <?:input>
str Are you serious?