I get an issue using a shell script on Ubuntu.

My script :

/usr/local/bin/node ./index.js
exec bash

I have set it as executable with:

sudo chmod +x ./index.sh

Then run it with:

sudo ./index.sh

But get:

sudo: unable to execute ./index.sh: No such file or directory

However, the file is there:

$ ls -la 
-rwxr-xr-x   1 root root    54 oct.   4 10:05 index.sh

closed as off-topic by Michael Homer, Archemar, sebasth, Jeff Schaller, αғsнιη Oct 4 '17 at 9:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Michael Homer, Archemar, sebasth, Jeff Schaller, αғsнιη
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Can you give us the result of a ls -al ? – Carpette Oct 4 '17 at 8:25
  • You don't need ./ when you are changing permissions and so sudo chmod +x index.sh. Also try running the command with the full path and so sudo <fullpath>/index.sh – Raman Sailopal Oct 4 '17 at 8:32
  • 1
    Did you by any chance edit the file on Windows? In this case, then newlines may be DOS newlines. Use dos2unix to remove these, or tr -d '\r' <index.sh >index.sh-new to filter all \r out. – Kusalananda Oct 4 '17 at 8:52
  • 1
    Please show us the output of both sudo $PWD/index.sh and sudo ls -l $PWD/index.sh and also sudo ls $PWD/index.sh | od -c. But add them to your question. Copy the output from your terminal exactly as it is and use the formatting tools to format it as code. Also, what happens if you try typing sudo $PWD/index and then hitting TAB? Also, what is the actual path? Are there any spaces in the names of folder1 or folder2? – terdon Oct 4 '17 at 8:54
  • 3
    Ok so dos2unix seems to be the solution. It works. – tonymx227 Oct 4 '17 at 9:05

The script was at some point edited on a Windows machine. Editors on Windows usually use DOS line-endings. These have an extra carriage return (\r) compared to Unix line-endings. These confuse the kernel when sudo asks it to run the script.

To fix the script, run dos2unix over it.

Alternatively, remove all \r in the script with

$ tr -d '\r' <index.sh >index.sh-new
$ # test index.sh-new to make sure it works
$ mv index.sh-new index.sh

This will obviously break the script if it relies on handling literal carriage returns in some way.

  • 1
    And there I was poised with the noexec mount of /var/www answer. (-: – JdeBP Oct 4 '17 at 9:16
  • 1
    For added fun, the error message arises because sudo is involved. sudo calls one of the exec syscalls, which fails with ENOENT because the interpreter doesn’t exist (with a \r), and that’s that; sudo prints the corresponding “no such file or directory” message. Running the script directly from a shell would give a “bad interpreter” error instead, because when a shell sees an error from exec, it reads the script itself and attempts to handle it directly: in this case it sees the shebang and also fails to find the interpreter, resulting in the “bad interpreter” error. – Stephen Kitt Oct 4 '17 at 9:48
  • @StephenKitt This depends on the shell though. ksh will, for example, not say "bad interpreter", but zsh and bash will. – Kusalananda Oct 4 '17 at 9:52
  • @Kusalananda it does indeed, thanks for checking ;-). – Stephen Kitt Oct 4 '17 at 9:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.