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 foundThe 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 $PATHRun 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.shAny 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 directlyIf 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.shNotes
- The extension does not matter to bash.
my-scriptandmy-script.shfail the same way as bare names - in both cases bash searched $PATH and found nothing. - Tab completion helps: type
./myand hit Tab to complete the name.