bad interpreter: No such file or directory

You sometimes see this error message when attempting to run a Perl, Python, or shell script that uses a shebang line to find the interpreter. For example,

$ hello.pl
-bash: hello.pl: command not found

This error message usually indicates a problem in the shebang line at the start of the script:

#!/usr/bin/perl
print "Hello Perl\n";

The most common cause of the problem is that the interpreter is not installed where you expect. For example, it’s in /usr/local/bin instead of /usr/bin. However, there’s a second problem that can cause this symptom, especially if you’re working on a Mac; and it’s not nearly so obvious. If the first line ends in just a carriage return rather than a linefeed or a carriage return linefeed pair, then bash gets very confused.

The fix is to modify the file so that it uses Unix linefeeds rather than Mac carriage returns to end each line. Some text editors like BBEdit allow you to configure this. You can also use the mac2unix utility bundled on most Unixes to accomplish this.

9 Responses to “bad interpreter: No such file or directory”

  1. Neville Rowe Says:

    Perl used to recommend not doing #!/usr/bin/perl to get the interpreter. Instead you did

    #!/bin/sh
    exec perl -w -x $0
    #!perl

    or some other such construct – sorry, it’s too long since I’ve done this regularly.

    This has the advantage that sh searches the path for Perl, rather than the location being coded in the script. -w is a special flag which tells Perl to scan for a #!perl line in the file and start executing from there. I appear to remember there is a slight downside that linenumbers when debugging are out slightly ?

  2. memememe Says:

    Thank you so much for posting this! I spent all morning trying to figure out if there was something wrong with my Perl install (I’m on a 10.3.9 machine).. I even checked the ADC website for notes about Perl and couldn’t find anything!! perl -e was working but reading from my file wasn’t. Finally I searched on the “bad interpreter” error and found your page. I switched to “Unix” in BBEdit and it fixed it. Thank you so so much!

  3. Rog Says:

    Thanks, I’ve never seen the construct Neville showed, it worked

  4. rajput Says:

    Thanks for the information for “bad interpreter: No such file or directory” error. I too got the same error. But i solved it in a different way. I had written a script in Windows and was trying to execute it on a device with linux kernel. What is did is changed my Windows scripts file to a UNIX format. And Yes!!! The error was gone :-)

  5. Tim_Myth Says:

    YOU ARE AWESOME!!! My webhost moved my site to a new server, and suddenly one of my cgi scripts quit working. I had the hosting company rebuild my site from a last known good back up, but still got the same problem. I poured over the code line by line, tried to figure out which directory was bad, and ripped a few hairs out (and there’s not many of those to spare!). I even lowered myself to asking one of my buddies for help. He’s a PhD candidate and the sacrifice was great, but I could not find my error. Even he could find no reason for the error (although he found much fault in my coding skills). I finally stumbled acrossed this post, and (not believing it could be that simple) had my site fixed in a matter of seconds.

  6. Ram Says:

    I was trying to run a script that I copied off the web and pasted into Notepad and saved to my Linux dev server. It had two problems – one, the shebang line referenced /usr/local/bin instead of /usr/bin/perl and two, the interpreter didn’t like my Windows linefeeds. I deleted the script and used vim to create the .pl file, corrected the shebang line and everything worked. Thank you for this very useful post that pinpointed both issues.

  7. Mark Says:

    Maybe you can help me I’m trying to write a simple input -> logfile script and keep getting the message “cannot create link to logfile: No such file or directory at /private/var/folders/EZ/EZjgQkjmGqen0F4rMB61CU+++TI/Cleanup At Startup/filehandletest-288592573.001.pl line 8, line 2.”

    but I can’t figure out what’s going wrong!

    my code is as follows:
    #!/usr/bin/perl
    use 5.010;
    use strict;
    print “Enter some text then hit control+D and we\’ll try to send it to the perlogfile:\n”;
    my @stuff = ;
    open LOG, “>>”, “/Users/rk/Documents/perlogfile.rtf”; # im getting permission denied here when trying to write to or create this file, wtf?
    if (! open LOG ) {
    die “cannot create link to logfile: $!”;
    }
    print LOG “@stuff”;
    close (LOG);

  8. guest Says:

    ./test.pl
    : No such file or directory

    The Problem could also be: The script has been written with windows-line-endings. So Linux can’t parse it…

  9. imanijit Says:

    Also, if you are moving your file from a Mac to a Linux machine, be aware that:
    #!/usr/bin/Perl
    and
    #!/usr/bin/perl

    do not mean the same thing to a case-sensitive file system like Linux.
    Not gonna admit to how long it took me to figure out why my script worked fine on my Mac and failed on my Linux box. >.<

Leave a Reply