Labrador Bash library. Collection of functions and libraries that I deem useful for working with Bash.
Install L_lib using your preferred Bash package manager:
basher install Kamilcuk/L_libbpkg install Kamilcuk/L_libshpkg install Kamilcuk/L_libThe library is one file. Download the latest release from GitHub and put in your PATH:
mkdir -vp ~/.local/bin/
wget -O ~/.local/bin/L_lib.sh https://raw.githubusercontent.com/Kamilcuk/L_lib/refs/heads/v1/bin/L_lib.sh
export PATH=~/.local/bin:$PATHpip install L_libbasher install Kamilcuk/L_libYou can use the library in scripts with:
. L_lib.sh -sUnless -n, sourcing the library will enable extglob and patsub_replacement and, if set -e is set and there is no ERR trap, it will also register a ERR trap that will print a nice traceback on unhandled error.
For example, a simple script using the library could look like:
#!/bin/bash
. L_lib.sh -s
L_log "Starting script"
L_info "This is an info message"You can test the library ad-hoc:
bash <(wget -qO- https://raw.githubusercontent.com/Kamilcuk/L_lib/refs/heads/v1/bin/L_lib.sh) L_setx L_log 'Hello world'Below is a selection of the library's features. The library contains much more.
- Argument parsing in Bash with short, long options, sub-parsers, sub-functions support and shell completion
L_argparse - Logging library with levels and configurable output and filtering
L_log_configureL_infoL_logrunL_run - Pretty function stack printing usually on ERR trap
L_print_traceback - Execute an action on EXIT, any terminating signal or RETURN trap of any function
L_finally - Create temporary directory and
cdto it for the duration of a function with auto removal after returnL_with_cdL_with_cd_tmpdir - Create unidirectional connected two file descriptors
L_pipe - Create and manage multiple processes with separate file descriptors for standard input and standard output
L_proc_popenL_proc_communicateL_proc_kill - Temporary enable or disable shell features
set -xfor the duration of a commandL_setxL_unsetxL_extglob - Easily sort a Bash arrays containing any characters
L_sort - Failure handling utilities
L_assertL_dieL_checkL_panic - Variables holding color codes depending on terminal support
L_color_detect$L_RED$L_BLUE - Checking Bash features and version
$L_BASH_VERSION$L_HAS_BASH4_0$L_HAS_COMPGEN_V$L_HAS_WAIT_N - Waiting on multiple PIDs with a timeout ignoring signals and collecting all exit codes
L_wait - Simplify storing exit status of a command into a variable
L_exit_toL_exit_to_10 - Help with path operations, with
PATHorPYTHONPATHmanipulationL_path_stemL_dir_is_emptyL_path_appendL_path_relative_to - String utilities
L_stripL_strupperL_strstrL_html_escapeL_urlencode - Split string without remote execution and understand
$''sequencesL_str_split - Template output
L_percent_formatL_fstring - JSON escape
L_json_escape - Cache commands execution with TTL in memory or file
L_cacheL_cache_decorate - Easy writing function utilities by supporting
-v <var>option or extracting comment before functionL_handle_v_scalarL_func_helpL_func_usage_errorL_decorate - All with support for any Bash versions from 3.2 to latest with portability functions
L_readarrayL_epochrealtime_usecL_compgen -V
Kindly feel free to have conversations and ask questions on GitHub discussion.
Report bugs using GitHub issue.
Contributions are welcome! You can run the tests locally with make test or check static analysis with make shellcheck. Please submit pull requests to the main repository.
L_*prefix for public symbols._L_*prefix for private symbols, including local variables in functions taking a name-reference.- Upper case used for global scope read-only variables.
- Lower case used for functions and user mutable variables.
- Snake case for everything.
- The option
-v <var>is used to store the result in a variable instead of printing it.- This follows the convention of
printf -v <var>. - Without the
-voption, the function outputs the elements on lines to standard output. - Associated function with
_vsuffix store the result in a hardcoded scratch variableL_v.
- This follows the convention of
- Return 2 on usage error, return 124 on timeout.
