Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

A shell script that exists says No such file or directory when you run it

Errorbash: ./script.sh: No such file or directory

Short 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.

  • Applies to bash 5, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 | head
  2. 2Match what you see, then jump to its fix

    1. 1od -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. 2The first line is something like #!/usr/bin/python or #!/bin/zsh on a machine without itThe interpreter in the shebang does not existFix
    3. 3The error appears for ./script.sh but ls shows the file under another folderThe path you typed is not the script's pathFix

What does the error look like?

  • bash: ./script.sh: No such file or directory
  • execve: 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.sh

2Fix

Convert the file to Unix line endings.

sed -i 's/\r$//' script.sh

3Verify

Run the script.

./script.sh

Confirmed by man7.org

Did this fix it?

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/bash

2Fix

Point the shebang at an interpreter that is installed, or install it.

#!/bin/bash

3Verify

Run the script.

./script.sh

Confirmed by man7.org

Did this fix it?

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.sh

2Fix

Run the script with its real path, or change into its folder first.

cd /path/to/folder
bash ./script.sh

3Verify

The script starts.

bash ./script.sh

Confirmed by man7.org

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.