My dotfiles build PATH from every plausible bin directory across Linux, macOS, Homebrew, and a pile of per-language install conventions—keeping only the directories that exist—without a single [ -d ] test:
IFS=; command -v setopt > /dev/null && setopt -o nullglob || shopt -s nullglob
matches=(
${ELLIPSIS}[b]in/.
${ELLIPSIS}data/profile/vim/plugged/*/bin/.
{$HOME/,/opt/homebrew/,/usr/{local/,},/}{,opt/}{,libpq/,.local/,go/,.cargo/,Library/Python/[0-9].[0-9]{,[0-9]}/}{s,}[b]in/{,[0-9A-Za-z]*/}.
)
printf '%s' "${matches[@]/%\/./:}$PATH"Brace expansion multiplies the candidate matrix—prefixes times interpositions times sbin-or-bin—into a few hundred candidate paths, and nullglob makes every candidate that matches nothing vanish.
That’s the plan. The trick is that nullglob only applies to words that contain a glob character. A literal like /usr/bin/. is not a pattern, so nullglob passes it through untouched whether it exists or not, and the matrix inevitably produces some candidates with no * or [...] left in them. Writing [b]in—a character class matching exactly b—forces every candidate to be a pattern, so every candidate gets existence-tested by the filesystem.
Two supporting details. Every candidate ends in /., which only a real directory can satisfy—a plain file named bin fails the match. And the join is a single expansion: "${matches[@]/%\/./:}" replaces the trailing /. with : on every surviving element, so printf '%s' prints them back-to-back as a finished PATH string with the old $PATH appended.
The first line is a polyglot: command -v setopt succeeds in zsh and fails in bash, so one line enables nullglob in whichever shell sourced the file. And the plugged/*/bin entry means vim plugins contribute binaries to PATH—fzf arrives that way, installed by the vim plugin manager and consumed by the shell.
The same one-character trick works as a bare existence probe. Elsewhere in the same dotfiles:
export DEVEL=$(
setopt -o nullglob
printf '%s\n' ~/deve[l] /deve[l] |
head -n1
)~/deve[l] expands to ~/devel only if it exists, and to nothing otherwise; the first surviving candidate wins. This version tests existence only: without the trailing /., a regular file can also match.
Cost: the matrix is computed once and cached in the environment—the surrounding code only rebuilds ELLIPSIS_PATH when it’s unset—so subshells never re-glob. The matrix grew through several revisions of my shell setup. Still how every one of my shells gets its PATH.