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:
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:
This will typically come back with a response like this:
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.
#!/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
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:
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.
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.
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.
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
Confluence: City Girl on : City Girl Development Environment
Show preview
City Girl Development Environment Liddles uses Git (
Comments
Display comments as Linear | Threaded
chrashoverraid on :
now its working :)
.xghost on :
OrthodoxCaveman on :
Andrei on :
Double O on :
$dos2unix name-of-file
should do the trick..
Hakan on :
Richa Sabharwal on :
Lee on :
Anil on :
[email protected]
Vladimir Tsukur on :
kirubakaran on :
VC on :
runbux on :
Anil K on :
:set fileformat=unix
and dos2unix
nothign dint worked for me.
?
- Anil K
Han Solo on :
Ravi on :
Craig Fairhurst on :
Kevin McCaughey on :
Thanks for taking the time to post this - I'm sure it has helped countless people a HUGE headache :)
Denver Green on :
Glendon on :
Blake on :
ahmed on :
Linux worker on :
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 :
Tanmay on :
Shindu on :
K0h4n on :
Maurice on :
Anne of No-Work Spanish on :
3l4d2 on :
Robert on :
Bazzard on :
Mike on :
David on :
fuzzycheck on :
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 :
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 :
pragathy on :
"
Command i run was: ./configure --enable-shared
DMH on :
Panda on :
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!