More Bash answers

by Danny
Published 3 min read

You installed a tool but the shell says command not found. Learn how PATH works, where to add directories, and when to run hash -r or reload your rc file.

The short answer

#

The shell cannot find the program because the directory it was installed into is not on your PATH, or your current shell has not picked up the change yet. Find where the binary lives, add that directory to PATH in your shell's startup file, then reload it.


_10
# Example: the tool was installed into ~/.local/bin
_10
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
_10
source ~/.bashrc
_10
hash -r

On macOS the default shell is zsh, so use ~/.zshrc instead of ~/.bashrc.

Why it happens

#

When you type a command name without a slash, the shell looks through each directory listed in the PATH variable, in order, and runs the first match. If none of them contain the program, you get command not found.


_10
echo "$PATH" | tr ':' '\n'

Package managers and installers often put binaries in places that are not on PATH by default, for example:

  • ~/.local/bin (pip with --user, pipx, many install scripts)
  • ~/.cargo/bin (Rust tools)
  • ~/go/bin (Go tools)
  • the global bin folder of npm, pnpm or a Node version manager
  • /opt/homebrew/bin on Apple Silicon Macs

Even when an installer edits your startup file for you, the shell you already have open read that file when it started. It will not see the change until you reload it or open a new terminal.

There is one more layer. Bash remembers where it found each command in a hash table, so it does not search PATH every time. If a command moved, or you installed a newer copy earlier in PATH, Bash may keep using the old location until you clear that cache.

How to fix it

#

Find where the program was installed

Check the installer's output first. If it scrolled away, these help:


_10
# Does the shell already know about it?
_10
command -v mytool
_10
type -a mytool
_10
_10
# Where did the package manager put it?
_10
npm prefix -g # npm global bin lives in this folder's bin
_10
pip show -f mypackage # lists installed files
_10
ls ~/.local/bin ~/.cargo/bin ~/go/bin 2>/dev/null

Add the directory to PATH in the right file

Put the export in the file your shell reads for interactive sessions:

ShellFile to edit
Bash on Linux~/.bashrc
Bash on macOS (login shells)~/.bash_profile
zsh (macOS default)~/.zshrc

_10
export PATH="$HOME/.local/bin:$PATH"

Putting the new directory first means its programs win over older system copies. Putting it last (PATH="$PATH:$HOME/.local/bin") means system copies win. Pick deliberately.

On many Linux distributions, ~/.bash_profile or ~/.profile sources ~/.bashrc, so editing ~/.bashrc covers both login and non-login shells. If you are unsure, check that file for a line that sources ~/.bashrc.

Reload the shell


_10
source ~/.bashrc # or: source ~/.zshrc

Opening a new terminal tab works too. Running exec "$SHELL" -l replaces the current shell with a fresh login shell.

Clear the command cache

If the command exists on PATH but the shell still runs an old copy, or reports No such file or directory for a path that has since moved, clear the hash table:


_10
hash -r # Bash
_10
rehash # zsh

Check scripts, cron and CI separately

Non-interactive shells, cron jobs, systemd services and CI runners do not read ~/.bashrc. If a command works in your terminal but not in a script or job, either use the full path to the binary or set PATH at the top of the script or job definition.


_10
#!/usr/bin/env bash
_10
export PATH="$HOME/.local/bin:$PATH"
_10
mytool --version

Common pitfalls

#
  • Using single quotes inside the export, such as export PATH='$HOME/bin:$PATH'. Single quotes stop expansion, so PATH becomes the literal text and almost every command breaks. Use double quotes.
  • Overwriting PATH entirely with export PATH="$HOME/bin", which removes /usr/bin and friends. Always include the existing $PATH.
  • Editing ~/.bashrc while your shell is actually zsh. Check with echo "$SHELL" or ps -p $$.
  • Adding the same line many times while experimenting. It works but makes PATH long and confusing; tidy it up.
  • Installing with sudo npm install -g or sudo pip install to make a command appear. This can create permission problems later. Prefer a user-level install directory on your PATH.
#

Was this resource helpful?