Ctrl K

Running Scripts from the Current Directory

Why a bare script name fails with "command not found" and how to run a file with an explicit path like ./my-script.sh.

Typing a bare script name in the script's own directory fails with "command not found" even though the file is right there. This is expected: bash resolves bare names only through $PATH, never through the current directory. This doc explains the lookup rule and the path forms that do work.

The error

cd ~/scripts
my-script.sh
# bash: my-script.sh: command not found

The file exists, but bash never looked at the current directory to find it.

Why bare names fail

When you type a bare name, bash searches only the directories listed in your $PATH (like /usr/bin). The current directory is not part of that search - a deliberate security default on Linux, so a malicious file named like a common command cannot hijack it.

echo $PATH

Run with an explicit path

Give the shell any explicit path and it stops searching $PATH and runs that file directly. The ./ prefix means "in this directory".

./my-script.sh

Any other path form works the same way - it just cannot be a bare name:

/home/you/scripts/my-script.sh   # absolute path
../scripts/my-script.sh          # relative path
bash my-script.sh                # pass the file to bash directly

If the file is not executable

The path forms above (except bash my-script.sh) also require the execute bit. If you get "Permission denied" instead of "command not found":

chmod +x my-script.sh
./my-script.sh

Notes

  • The extension does not matter to bash. my-script and my-script.sh fail the same way as bare names - in both cases bash searched $PATH and found nothing.
  • Tab completion helps: type ./my and hit Tab to complete the name.