All articles

Debug PATH, Aliases, Functions, and Command Caches

8 minutes read


Linux publication

Share this article

𝕏✉

Illustration for Debug PATH, Aliases, Functions, and Command Caches

Type a command name and the shell may never search PATH. It can expand an alias, call a function, or select a builtin first. Bash may also reuse a pathname from its hash table. When the wrong program runs, inspect the shell’s choice before reinstalling anything.

The workflow was tested with Bash 5.2 on Ubuntu 24.04. It labels the parts that are portable to a POSIX shell and the parts that are Bash-specific.

Ask the current shell first

POSIX defines these two inspection forms:

command -v python
command -V python

command -v emits the value the current shell would use; command -V adds a human-readable category. Depending on the name, that result may be a pathname, function, builtin, or alias 1.

In Bash, get every known candidate:

type -a python
type -a printf

type -a includes all locations found through PATH, while type without -a reports the winning interpretation 3. Run these in the shell that exhibits the problem. A subprocess cannot reliably see its parent’s functions, aliases, or hash state.

Separate aliases, functions, and builtins

alias python 2>/dev/null || true
declare -F python 2>/dev/null || true
type -a python

declare -F is Bash-specific. Aliases are normally expanded while parsing an interactive command; command python suppresses function lookup and alias substitution for that command position, but it does not promise a separate external executable in every shell 1.

To run the exact file you inspected, invoke its path:

/usr/bin/python3 --version

That bypasses PATH, aliases, and same-named functions. It also makes the chosen location an explicit dependency.

Read PATH in order

printf '%s\n' "$PATH" | tr ':' '\n' | nl -ba

The first matching executable normally wins during Bash’s path search 2. Check the candidate itself:

name=python3
candidate=$(command -v "$name") || exit 1
ls -l -- "$candidate"
file -- "$candidate"

In common shells, an empty PATH field represents the current directory and is easy to miss. Any writable directory early in PATH is a security boundary: someone who can create a same-named executable there can redirect later commands.

For a standards-defined utility path, POSIX provides:

command -p getconf PATH

That is useful when the user’s PATH is itself broken 1.

Refresh the shell’s remembered paths

Shells may remember executable locations to avoid a new search. Inspect and clear that cache with:

hash -t python3 2>/dev/null || true
hash -r

POSIX defines hash with no options and hash -r, which forgets every remembered utility location 4. Both forms work in Dash and other POSIX shells, so hash -r is safe in a /bin/sh script after it changes PATH. Only hash -t and the other options (-d, -l, -p) are Bash extensions 5. hash -r affects only the current shell process; it does not edit PATH or shell startup files.

The isolated PATH lab

This lab creates one executable in a fresh directory and changes PATH only for one child Bash:

lab=$(mktemp -d)
mkdir "$lab/bin"
printf '%s\n' '#!/bin/sh' 'printf "lab-demo\n"' > "$lab/bin/lab-demo"
chmod 700 "$lab/bin/lab-demo"

PATH="$lab/bin:$PATH" bash -c '
  command -V lab-demo
  type -a lab-demo
  lab-demo
'

Observed output:

lab-demo is /tmp/tmp.example/bin/lab-demo
lab-demo is /tmp/tmp.example/bin/lab-demo
lab-demo

Caution: Remove only the fresh directory named by $lab; verify it is non-empty and starts with your temporary-directory prefix before cleanup.

case "$lab" in
  /tmp/tmp.*) rm -f -- "$lab/bin/lab-demo" && rmdir -- "$lab/bin" "$lab" ;;
  *) printf '%s\n' 'refusing unexpected cleanup path' >&2; exit 1 ;;
esac

The lab does not source dotfiles or persist environment changes.

A compact incident sequence

  1. Run command -V name in the affected shell.
  2. In Bash, run type -a name and inspect aliases/functions.
  3. Print PATH one entry per line.
  4. Inspect the winning file and its interpreter.
  5. Clear the shell’s hash table only if the path changed.
  6. Invoke an absolute path when reproducibility matters.

Establish what the shell will execute before checking which package owns the command.

Sources and further reading
  1. POSIX.1-2024 — command
  2. GNU Bash manual — Command Search and Execution
  3. GNU Bash manual — Bash Builtins
  4. POSIX.1-2024 — hash
  5. GNU Bash manual — Bourne Shell Builtins