Skip to content

Linux shell scripting: bad interpreter: No such file or directory

This error pops up for a couple of reasons. At the top of the script there will probably be a line that looks like this:

#!/bin/sh


This is telling Linux that this script should be interpreted using the /bin/sh program. So your first step is to verify that program exists. I tend to use:

which sh


This will typically come back with a response like this:

/bin/sh


This is telling us that the path to the sh program is in fact /bin/sh, matching the path specified at the top of the script. Ok, so what gives? Well, it's possible that this script was made on an operating system that has line ending characters different than linux. This could have been on on a Mac or PC, or the file could have been converted when it was packaged. In this case, you get the relatively misleading bad interpreter: No such file or directory message, which is really trying to look for sh, although you don't get any indication of the fact.

So, how to fix? Read on.
There are various ways to fix the problem, but I find one of the simplest being the use of vi which is standard on most unix systems, and in linux comes in the form of the vim package. Load the script up in vim, by typing vi filename

vi is a text based dinosaur in the day of wysiwyg editors, so if you don't know your way around, make sure you follow these steps carefully.

Once the file is loaded type:

:set fileformat=unix


And hit Enter/Return.

You won't notice anything, but the file has already been fixed. Now all you need to do is save and exit.

:wq!


Again Return, and you should be back in your shell. Run the shell script, and if all goes well, it should now execute properly, and without the dreaded bad interpreter: No such file or directory message.
Defined tags for this entry: , , , ,

Trackbacks

news.puppin.org on : PingBack

Show preview

Elsa Machado on : Elsa Machado

Show preview
Great article post.Thanks Again. Much obliged.

forums.hardwarezone.com.sg on : PingBack

Show preview

Comments

Display comments as Linear | Threaded

chrashoverraid on :

*Thank you, i am a linux noob and had this problem!
now its working :)

.xghost on :

*I encountered this problem too. The funny thing is that the script was written in the linux environment. Still had to fix it. No clue as to how it got contaminated with that in the first place :o

OrthodoxCaveman on :

*Happens the same with me, can't find any info on it too...

Andrei on :

*Great fix, thanks!

Double O on :

*I think even,
$dos2unix name-of-file
should do the trick..

Hakan on :

*Great fix, I appreciate it. Regards

Richa Sabharwal on :

*Thanks a ton!! I had the same problem. The script was sent to me as an attachment in the email. The fix worked like a charm for me on Snow Leopard OSX.

Lee on :

*You are correct sir!

kirubakaran on :

*Thanks a lottttttttttt, it works wellllll

VC on :

*Thank you!!!! This helped me a lot!!

runbux on :

*Didn't work. The Linux file structure is so arbitrary and incomprehensible -- On Purpose! Why can't it just work?

Anil K on :

*its not wokring now, i have a ksh and when i do
:set fileformat=unix
and dos2unix

nothign dint worked for me.

?
- Anil K

Han Solo on :

*This works ! Awesome, TY

Ravi on :

*Wow......... that was a greate information.. It helped me alot.. Thank you very much....Have a greate day

Kevin McCaughey on :

*Thank you so much!!! I had spent hours trying to get my script to work and eventually stumbled on your blog. How could I be so stupid? lol.

Thanks for taking the time to post this - I'm sure it has helped countless people a HUGE headache :)

Denver Green on :

*Solved my problem. Thank you for posting this information!!!

Glendon on :

*Thank you. Worked perfectly for me.

Blake on :

*Thank you, this saved me some headache.

ahmed on :

*thanks it work great

Linux worker on :

*Hi,
I have a problem when I did the following step:
******************
auxin@auxin-DQ45CB:~/Downloads/Propagation1d$ /Chaste.sh ChasteParameters.xml
bash: path_to_chaste: No such file or directory
auxin@auxin-DQ45CB:~/Downloads/Propagation1d$
******************
I don't know how to fix the problem,I tried several method to set path but it didn't work.

I would be happy if you help me to resolve this problem.

chris on :

*Thanks David, you saved me a day's work figuring this out

Tanmay on :

*Great!!!

Shindu on :

*Thx, great info.

K0h4n on :

*It works like a charm. Thank you for the solution. ^^

Maurice on :

*I works for me as well; thank you very much.

Anne of No-Work Spanish on :

*A million thank-you's. I was editing a file on someone else's system. Being the Linux-challenged person, I had transferred it over to Windows, edited and transferred back. I was going round and around and finally Googled the error message, which brought me here. 3 minutes later I'm back in business. Ginormous Gracias!

3l4d2 on :

*thanks! works like a charm!

Robert on :

*I had big problems trying to run python script written under Windows on Debian. I've searched many sites and finally I've found your solution. It is working like a charm. Thank you very much.

Mike on :

*Great, works perfectly thank-you!

David on :

*Awesome! Thanks for this article. Saved me some headache.

fuzzycheck on :

*Hi,
the procedure didn't work for me, but I found the reason: the file must not have a BOM, in addition to being in Unix format.

I create my scripts in Notepad++, and it gives the option of saving files in UTF8 format with a BOM, or without a BOM. The BOM adds a few characters at the beginning of the file.

You can check with hexdump:
a file with BOM:
hexdump -C compile_gtk.sh
00000000 ef bb bf 23 21 2f 62 69 6e 2f 73 68 0a 0a 46 49 |...#!/bin/sh..FI|

same file without BOM:
> hexdump -C compile_gtk.sh
00000000 23 21 2f 62 69 6e 2f 73 68 0a 0a 46 49 4c 45 3d |#!/bin/sh..FILE=|

The difference is the first 3 bytes "ef bb bf".

In VI, to check if the file has a BOM, type ":set bomb?". A message "bomb" or "nobomb" should appear.

To remove the BOM, type ":set nobomb", then save the file. Check again with hexdump that the BOM was removed.

zz on :

*Thanks for posting this fix!

Haven't used *nix in a while but it's now slowly becomming a bigger part of my life...

(lots of memory work)

Jim on :

*Thank you for posting this. I spent two days of frustration and desperation trying to get a simple cron working. You saved me from losing my mind! :-)

pragathy on :

*I too faced the same issue. After following ur steps that issue got fixed but now i am getting a new error: "./configure: 2: ./configure: ./aconfigure: not found
"
Command i run was: ./configure --enable-shared

DMH on :

*YOU are the GREATEST!!!!

Panda on :

*omg.

Unix line endings! working on windows, creating a php file that's not executing properly - spending hours trying to figure out why (because php executes the file properly, but the php interpreter isn't)

Thank you so much!

Add Comment

Pavatar, Gravatar, Favatar, MyBlogLog, Pavatar author images supported.
BBCode format allowed
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
Form options