Libraries#
Zsh has three layers of re-use. Built-ins live inside the
zsh binary. Loadable modules ship with zsh and attach more
built-ins on demand. Autoloaded functions are shell-language
files on $fpath that zsh parses the first time they are
called. On top of those, the operator can source plain files
and pull in external programs through $PATH.
Built-in Helpers#
Common built-ins worth knowing.
print, format and print (zsh-spelledechowith sane options like-l,-u2,-r).printf, C-style format strings.read, read a line from a stream.trap, run a handler on signals orEXIT.getopts, parse short flags (POSIX).setopt/unsetopt, toggle shell options.zstyle, the configuration system for completion and contrib functions.
Loadable Modules#
Compiled C modules ship with zsh and attach more built-ins on
demand. Load with zmodload zsh/<name>. zmodload -L lists
the load-on-demand mappings; zmodload with no arguments
lists what is loaded.
Module |
What it adds |
|---|---|
|
|
|
Native math: |
|
PCRE backend for |
|
|
|
TCP client / server through |
|
Unix-domain sockets through |
|
|
|
Profile shell startup and function calls |
|
|
|
Expose internals ( |
|
Menu-select completion UI |
|
Bind zsh to |
$ zmodload zsh/datetime
$ print -- "$EPOCHSECONDS"
$ zmodload zsh/mathfunc
$ print -- "$(( sqrt(2.0) ))"
$ zmodload zsh/parameter
$ print -l -- ${(k)builtins} | head
Autoload Functions#
autoload -Uz NAME defers loading a function; the body is the
file named NAME on $fpath. The function is parsed on
first call, not at shell startup, so heavy helpers don’t bloat
init time.
$ fpath=(~/.zsh/functions $fpath)
$ autoload -Uz add-zsh-hook bashcompinit run-help
The -U flag disables alias expansion inside the function (a
common source of bugs); -z opts into zsh-style parsing.
Always use -Uz for autoloaded code.
Zsh’s contrib library lives under
${prefix}/share/zsh/${version}/functions and ships dozens of
useful helpers. run-help opens man pages from the prompt;
zmv is a globbing rename; zcalc is a calculator REPL;
add-zsh-hook wires functions into precmd /
preexec cleanly.
$ autoload -Uz zmv
$ zmv '(*).JPG' '$1.jpg' # rename .JPG → .jpg in-place
$ autoload -Uz zcalc
$ zcalc -e '22 / 7'
External Tools#
Most “library” functionality is the same coreutils + friends toolkit bash leans on.
Text:
grep,sed,awk,cut,tr,sort,uniqFiles:
find,xargs,stat,rsyncHTTP:
curl,wgetJSON:
jqProcess:
ps,pgrep,kill,timeout
See Networking for the network-flavored slice and Coreutils for the catalog.
Sourcing a Module#
source FILE (or .) reads and executes a file in the
current shell. Every function, variable, and trap it
defines stays in scope after the file returns.
$ # ~/.zsh/lib/log.zsh
$ log::info() { print -- "[INFO] $*" }
$ log::warn() { print -u2 -- "[WARN] $*" }
$ source ~/.zsh/lib/log.zsh
$ log::info "starting"
The defensive idiom that protects against double-source.
$ # ~/.zsh/lib/common.zsh
$ [[ -n "${COMMON_LOADED:-}" ]] && return 0
$ COMMON_LOADED=1
$ log() { print -u2 -- "[$(date +%FT%T)] $*" }
References#
man 1 zshmodules(compiled C modules).man 1 zshcontrib(autoload functions in theFunctionssubdirectory of the zsh distribution).man 1 zshbuiltins(every shell builtin).Frameworks for the third-party plugin managers (Oh My Zsh, Prezto, zinit, antidote) that automate
$fpathand autoload setup.Overview for the language-level
source/autoloaddiscussion.