Lesson 6 of 14 · 20m
PATH, Packages, and Which Binary Actually Runs
Resolution order, bash's lookup cache, `/usr/local/bin` vs `/usr/bin`, shebang mechanics, and the version-manager shims that prepend your PATH.
Type python and press Enter. Which python runs? The answer is a colon-separated string in $PATH, searched left to right, plus a lookup cache bash keeps so it doesn't have to search every time. When the "wrong" binary runs, it's one of three things: an earlier PATH entry won, bash is using a hashed location that no longer exists, or you edited an rc file this shell never reads.
Resolution order and the honest commands
A command name resolves in order: alias → keyword → function → builtin → $PATH lookup. type -a python shows every match in resolution order — the alias shadowing the function shadowing three PATH entries. which only searches PATH, so it confidently reports /usr/bin/python while your shell runs a function or alias. command -v respects the full order and is what scripts should use; command ls also bypasses an ls alias when you need the real binary.
Bash caches successful lookups in a hash table. hash -r clears it — the fix for the classic "I installed the new version and it still runs the old one" after the binary moved. The command was resolved once, cached, and bash never re-searched.
Where binaries live
/usr/bin— distro package binaries (apt/dnf/pacman own these)./usr/local/bin— administrator-installed software, ahead of/usr/binon purpose.~/bin,~/.local/bin— your scripts andpip --userinstalls./opt/homebrew/bin— Homebrew on Apple Silicon;/usr/local/binon Intel Macs;/home/linuxbrew/.linuxbrewfor Homebrew-on-Linux. An rc file that hardcodes the wrong brew path is a subtle cross-machine bug./nix/store/...— Nix, where every package lives under its own hash, which is the entire point: two versions coexist without colliding.
dpkg -S "$(which nginx)" names the package that owns a file; apt-file/dpkg -L list a package's contents. Debian's update-alternatives manages the symlink farms for editor, java, python — --config editor picks the default. Install scripts that branch by system read /etc/os-release (ID=ubuntu, VERSION_ID) for the distro and uname -s/uname -m for OS and architecture — the three pieces of ground truth every portable installer needs.
The shebang line
#!/usr/bin/env bash resolves the interpreter through PATH — portable, because it finds bash wherever it lives (NixOS famously has no /bin/bash). #!/bin/bash is absolute: predictable, broken on systems without it. The kernel reads #!, hands the file to that interpreter. When chmod +xed file says No such file or directory anyway, the interpreter named in #! is what's missing — or the file has CRLF line endings and the kernel is looking for a program literally named bash\r. file script.sh and sed -i 's/\r$//' diagnose and fix it. env -S in a shebang allows arguments (#!/usr/bin/env -S bash -e) on modern coreutils.
Version managers
nvm, pyenv, rbenv all work the same way: shim directories pushed to the front of PATH from your rc file. They cost shell-startup time and re-resolve on every call. mise (Rust, asdf-plugin compatible, formerly rtx) is the current-generation answer — one tool for node/python/go/etc. The venv rule everyone should internalize: activation just prepends venv/bin to PATH. venv/bin/python script.py works without activating — the activation ceremony is optional.
macOS 12.3 (March 2022) removed the bundled python2, and every #!/usr/bin/env python script on updated Macs broke overnight. Shebang and PATH decisions are exactly the kind of thing that "worked until the OS update."
Practice
type -abeforewhich;hash -rafter moving or replacing binaries.#!/usr/bin/envshebangs for portable scripts; absolute shebangs when you control the box.- System package manager for tools, version manager for interpreters, venv for project dependencies.
- When an rc edit "does nothing," check which file that shell actually reads — login vs non-login is the usual answer (the dotfiles lesson has the full map).
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Red Hat Called This Bug a 5.9. Two Weeks Later It Was a 9.8.
CVE-2026-98432 is a use-after-free in the IPv4 TCP handshake, reachable by anybody who can reach a listening port, sitting in the kernel since 4.4. Red Hat scored it 5.9 moderate and deferred the fix. Then 8.1. Then 9.8.
Self-Host OpenClaw: VPS Setup to Production (2026)
Self-host OpenClaw from zero to production. Server provisioning, SSH lockdown, Docker, reverse proxy, Fail2ban, performance tuning, and full backup strategy in one guide.
Self-Hosting Prometheus and Grafana: Infrastructure Monitoring That Actually Works
Build a production-grade monitoring stack with Prometheus and Grafana from scratch. Covers architecture, Docker Compose deployment, Node Exporter, alerting with Alertmanager, PromQL from basics to advanced, pre-built dashboards, and everything SudoSecurity uses to monitor its own infrastructure.
Zero Trust Everything: The Complete Guide to Self-Hosting Teleport for Secure Infrastructure Access
SudoSecurity runs Teleport internally for everything — admin panel access, secure database connections, Kubernetes, and zero-trust philosophy across our entire team. This is the complete technical guide to installing, configuring, and running your own self-hosted Teleport cluster from scratch.