L_lib.sh¶

Labrador Bash library. Collection of functions and libraries that I deem useful for working with Bash.
Installation¶
The library is one file. Download the latest release from GitHub and put in your PATH:
mkdir -vp ~/.local/bin/
curl -o ~/.local/bin/L_lib.sh https://raw.githubusercontent.com/Kamilcuk/L_lib/refs/heads/v1/bin/L_lib.sh
export PATH=~/.local/bin:$PATH
You can use the library in scripts with:
. L_lib.sh -s
Unless -n, sourcing the library will enable extglob and patsub_replacement and, if set -e is set, register a ERR trap that will print a nice traceback on unhandled error.
You can test the library ad-hoc:
bash <(curl -sS https://raw.githubusercontent.com/Kamilcuk/L_lib/refs/heads/v1/bin/L_lib.sh) L_setx L_log 'Hello world'
Features¶
Below is some list with some of the library 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
Talk to me¶
Kindly feel free to have conversations and ask questions on GitHub discussion.
Report bugs using GitHub issue.
Conventions¶
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
License¶
LGPL