A shell script that exists says No such file or directory when you run it
bash: ./script.sh: No such file or directoryShort answer
When a script exists but the shell says no such file or directory, the missing file is usually the interpreter named on the first line, not the script. Check the shebang for a wrong path or a hidden carriage return from Windows line endings, correct it, and run the script again.
Fix it fast
1Find out which cause you have
Print the first line with every hidden character shown. A trailing \r means Windows line endings.
head -n 1 script.sh | od -c | head2Match what you see, then jump to its fix
- 1
od -c shows \r before \n on the first line, and the file came from Windows or a Windows editorThe shebang line ends in a carriage returnFix - 2
The first line is something like #!/usr/bin/python or #!/bin/zsh on a machine without itThe interpreter in the shebang does not existFix - 3
The error appears for ./script.sh but ls shows the file under another folderThe path you typed is not the script's pathFix
- 1
What does the error look like?
bash: ./script.sh: No such file or directoryexecve: ENOENT, the path or a script or ELF interpreter does not exist
Cause 1 of 3
The shebang line ends in a carriage return
1Check
The kernel takes the interpreter path from the first line. With a trailing carriage return the name it looks for is /bin/bash plus that character, which does not exist, so the call fails with ENOENT.
file script.sh2Fix
Convert the file to Unix line endings.
sed -i 's/\r$//' script.sh3Verify
Run the script.
./script.shConfirmed by man7.org
Cause 2 of 3
The interpreter in the shebang does not exist
1Check
execve fails with ENOENT when the script or its ELF interpreter does not exist. Check that the interpreter path is real.
head -n 1 script.sh
ls -l /bin/bash2Fix
Point the shebang at an interpreter that is installed, or install it.
#!/bin/bash3Verify
Run the script.
./script.shConfirmed by man7.org
Cause 3 of 3
The path you typed is not the script's path
1Check
execve also returns ENOENT when the path itself does not exist. Check the current folder and the file name.
pwd
ls -l script.sh2Fix
Run the script with its real path, or change into its folder first.
cd /path/to/folder
bash ./script.sh3Verify
The script starts.
bash ./script.shConfirmed by man7.org
How do I stop it coming back?
- Set the editor to save shell scripts with Unix line endings.
- Name an interpreter in the shebang that exists on every machine the script runs on.
- Check scripts that come from Windows with file before running them.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Servers
apt-get does not exist on this EC2 instance
sudo: apt-get: command not found
apt-get belongs to Debian and Ubuntu.
2 causes, each with a fixServers
bash warns that it cannot change the locale after you log in to a server
bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
The shell was told to use a locale that is not built on the server.
3 causes, each with a fixServers
Syntax error: redirection unexpected when a Bash script runs under sh
script.sh: 12: Syntax error: redirection unexpected
The operators <<< (here-string) and &> are Bash features.
3 causes, each with a fixServers
ping is missing inside a Docker container
sh: 1: ping: not found
Base images such as ubuntu are minimal, so ping is simply not installed.
2 causes, each with a fixServers
Running ps inside a container says command not found
bash: ps: command not found
Slim Debian and Ubuntu images do not ship the procps package, and ps comes from procps.
2 causes, each with a fixServers
GitHub Actions step fails writing to GITHUB_OUTPUT
Error: Invalid format '...'
The runner reads GITHUB_OUTPUT line by line, and every entry must be name=value or a name<<DELIMITER block.
3 causes, each with a fix