Learning about Bash builtins
I've spent the last couple of days consulting the docs and various forums to understand more about bash's own builtin commands. I run Fedora, and the ones that are enabled by default are the only ones accessible to you, point blank, unless you build bash from source and find the extras in examples/loadables. There's a few good utilities in there, including replacements for a lot of what would usually be a fork out to a separate process for a very basic command, such as cat.
I decided to build them all and then enable each one manually (enable -f /path/to/loadable loadablename) and have a play around with it, but quickly realised if I wanted to use these more regularly I'd have to make things easier for myself. First step was learning about the $BASH_LOADABLES_PATH variable, containing various folders to search for additional loadable builtins, thus negating the need for the -f flag - you can enable them like the default ones, with enable builtinname. I added $HOME/.local/lib/bash to this list in my .bashrc so I could add local builtins, like so:
if ! [[ "$BASH_LOADABLES_PATH" =~ "$HOME/.local/lib/bash" ]]; then
BASH_LOADABLES_PATH="$HOME/.local/lib/bash:$BASH_LOADABLES_PATH"
export BASH_LOADABLES_PATH
fi
Great! We can enable them easier now. But what if there were a global function we could call within the terminal and/or any script we wrote to crawl through all the directories in BASH_LOADABLES_PATH and automatically enable any builtins it found in there? Potential security nightmare if you're not in full control of what's on your system, but a fun exercise regardless. This is what I ended up putting in my .bashrc:
BASH_ENABLE_ALL_LOADABLE_BUILTINS() {
local IFS=':' loadable_builtins=() path path_array=()
read -a path_array <<< "$BASH_LOADABLES_PATH"
for path in "${path_array[@]}"; do
[[ "$path" != "." ]] || continue # exclude current dir
[[ -d "$path" ]] || continue # ignore nonexistent dirs
cd "$path" &>/dev/null || continue # enter loadables dir
loadable_builtins+=(*) # add contents to builtins array
done
[[ $* =~ ^-l$ ]] \
&& { printf '%s\n' "${loadable_builtins[@]}" ; return ; }
builtin enable ${loadable_builtins[@]}
}
export -f BASH_ENABLE_ALL_LOADABLE_BUILTINS # to call in scripts
It runs through all paths defined in BASH_LOADABLES_PATH, and makes use of glob expansion to build an array of the contents of all those paths.