Libraries#

Bash has no package manager of its own. Re-use is achieved by sourcing other shell files, calling external programs, or distributing helper functions in shared directories.

Built-in Helpers#

Common builtins worth knowing:

  • printf, format and print

  • read, read a line from a stream

  • mapfile / readarray, read lines into an array

  • trap, run a handler on signals or EXIT

  • getopts, parse short flags

External Tools#

Most “library” functionality comes from coreutils and friends:

  • Text: grep, sed, awk, cut, tr, sort, uniq

  • Files: find, xargs, stat, rsync

  • HTTP: curl, wget

  • JSON: jq

  • Process: ps, pgrep, kill, timeout

Sourcing a Module#

$ log::info() { printf '[INFO] %s\n' "$*"; }
$ log::warn() { printf '[WARN] %s\n' "$*" >&2; }
$ source "$(dirname "$0")/lib/log.sh"
$ log::info "starting"