Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Bash: Fix "Command Not Found" in Scripts

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

A "command not found" error in a bash script means the shell could not locate the executable you tried to run. The fix is almost always one of three things: the command is not installed, it is not on the script's PATH, or the script is calling it by the wrong name. Check those three in that order and the error usually disappears in a minute or two.

What You'll Need

  • Access to the machine running the script (local terminal or SSH)
  • The script file itself, so you can read the exact line that fails
  • Permission to install packages if a tool turns out to be missing

Step-by-Step

  1. Read the exact error. Run the script and note the line number and the command name in the message, for example ./backup.sh: line 12: rsync: command not found. The name before the colon is what bash could not find.
  2. Confirm the command exists on the system. In an interactive shell, run command -v rsync (replace with your command). If it prints a path, the tool is installed and the problem is environment-related. If it prints nothing, the tool is missing.
  3. Install the missing tool if needed. Use your distribution's package manager, for example sudo apt install rsync on Debian/Ubuntu or sudo dnf install rsync on Fedora. On macOS, use Homebrew if it is installed. After installing, run command -v again to confirm.
  4. Check the script's PATH. Add a temporary debug line near the top of the script: echo "$PATH". Compare it with the PATH in your interactive shell (echo $PATH). If the script's PATH is shorter, that is your cause. Scripts run from cron, systemd or a minimal shell often get a very small PATH.
  5. Fix the PATH inside the script. Add an explicit line before the failing command, such as export PATH="/usr/local/bin:/usr/bin:/bin:$PATH". This is the most reliable fix for cron jobs and services, because it does not depend on the caller's environment.
  6. Check for a typo or wrong case. Linux command names are case-sensitive. Python is not python, and Git is not git. Compare the name in the script with the output of command -v.
  7. Check the shebang line. The first line of the script should be #!/bin/bash or #!/usr/bin/env bash. If it is missing or points to a shell that does not have the command on its PATH, the script may run under a different environment than you expect.
  8. Run the script with bash explicitly. Try bash ./yourscript.sh instead of ./yourscript.sh. If it works now, the executable bit or shebang is the issue, not the command itself.
  9. Re-test in the same context that failed. If the error came from cron, run the job manually with the same environment (or check the cron mail/log). If it came from systemd, check the unit's Environment= and ExecStart= lines. Fixing it in your terminal does not prove it is fixed for the scheduler.

Common Mistakes

  • Assuming the tool is installed because it works in your terminal. Your terminal has a login PATH; cron and services usually do not.
  • Using a relative path like ./tool when the script's working directory is different from where you tested it. Use an absolute path or cd to a known directory first.
  • Forgetting that sudo resets PATH in many configurations, so a command found as your user is not found under sudo.
  • Editing the script but running an old copy, or a copy on another machine. Confirm you are editing and running the same file.
  • Quoting the command name incorrectly, such as "rsync" with stray spaces or a non-breaking space pasted from a document.

Tips

  • Put an explicit export PATH=... line at the top of any script that will run unattended.
  • Use command -v rather than which inside scripts; command -v is a shell builtin and behaves consistently.
  • Add set -euo pipefail near the top so a missing command stops the script instead of letting it continue with partial results.
  • Keep a short comment next to any absolute path explaining why it is there, so future edits do not remove it.

Troubleshooting

If command -v finds the tool but the script still fails, the script is running under a different user or environment. Check the user with id and the PATH with echo $PATH from inside the script. For cron, remember that the job runs with the crontab owner's environment; for systemd, check the unit file. If the tool is installed in a non-standard location such as /opt or a user's home directory, either add that directory to PATH in the script or call the binary by its full path. If you installed the tool after the script started, re-run the script; a running process does not pick up new PATH entries. Finally, confirm the script file itself is not being run from a different directory than you think, for example by adding pwd temporarily.

Frequently Asked Questions

Why does the command work in my terminal but not in the script?

Your interactive shell loads a PATH from your profile files. Scripts, cron jobs and services often start with a minimal PATH that does not include the directory where the tool lives. Set PATH explicitly inside the script.

How do I find where a command is installed?

Run command -v name in a shell where it works. It prints the full path. Use that path in the script if you do not want to change PATH.

Does the shebang line affect which commands are found?

Yes. The shebang chooses the interpreter, and that interpreter inherits the environment it was started with. A missing or wrong shebang can make the script run under a shell with a different PATH.

Why does it fail only under sudo?

Many systems reset PATH when switching users with sudo. Use the full path to the binary, or configure sudo to preserve PATH if your security policy allows it.

Can a typo cause "command not found"?

Yes. Command names are case-sensitive on Linux and macOS. Check the spelling and case against the output of command -v.

Is it safe to add a directory to PATH permanently?

Only add directories you trust. A writable directory early in PATH lets another user replace a command with their own program. Prefer absolute paths in scripts that run as root.