Technology, Social Media, Travel
RSS icon Email icon Home icon
  • Fixing a “: bad interpreter: No such file or directory” Error When You Know the Interpreter Path Is Correct.

    Posted on April 3rd, 2005 John Berns 7 comments

    When I ran a new Perl script from the command line (like this):

    ./scriptname.pl

    I got the following error:

    : bad interpreter: No such file or directory

    Usually that error means that the path to the Perl interpreter is incorrect.

    (If you are a Perl newbie, check that the first line of your program contains something like

    #!/usr/bin/perl

    and there really is a perl interpreter at that location! How can you tell that you have the right path? See below for that tip.)

    However, in this case, the script’s first line was #!/usr/bin/perl which I knew was the correct path to the PERL interpreter.

    So why was I getting this error? It turned out that the culprit was a DOS carriage return character somewhere in the script that was causing the problem!

    Solutions:

    1) Don’t use a Windows-based editor to edit PERL programs!

    2) Run dos2unix on the file to remove the DOS carriage return:

    dos2unix scriptname.pl

    TIP: How to tell you have the right PERL path.

    Well, the common paths to the perl interpeter are /usr/bin/perl and /usr/local/bin/perl though, I am sure there are plenty of variants out there.

    You can test the path (minus the “#!” of course) to see if it is correct by simply entering the path you THINK is correct, followed by “-v” :

    /usr/bin/perl -v

    If the path is correct you will get a version message something like this:

    This is perl, v5.6.1 built for i686-linux

    Copyright 1987-2001, Larry Wall

    Perl may be copied only under the terms of either the Artistic License or the
    GNU General Public License, which may be found in the Perl 5 source kit.

    Complete documentation for Perl, including FAQ lists, should be found on
    this system using `man perl’ or `perldoc perl’. If you have access to the
    Internet, point your browser at http://www.perl.com/, the Perl Home Page.

     

    7 responses to “Fixing a “: bad interpreter: No such file or directory” Error When You Know the Interpreter Path Is Correct.” RSS icon

    • Hi,

      I find your explanation to my problem is easier to understand than what i got from going to google.com. Thanks. I will try this one.

    • Better yet dont use Windows at all…

    • John, I have been coding Perl by hand now using a text editor call NoteTab and its excellent. Under document types you can save as a “Unix type” and it removes all the Ctrl-Ms that are plagueing you so much.

      It can also do massive global and search replace options with countless documents.
      I have not as of yet discovered a PERL rapid development tool that I like.

    • I don’t know how I could have diagnosed this error without your explanation – thank you very much.

      It’s weird that Xemacs on SUSE 10 would be adding DOS CR’s to my file. I really don’t want to switch to vi despsite how great my co-workers think it is.

    • You can always use gvim – if you need a gui, or kwrite/kate if you absolutely can’t stand vim.

    • Thanks – this saved me a lot of head scratching. I couldn’t see any telltale ^M characters using vi on RHEL4 but the clue was at the bottom of the window with the subtle “[dos]” text displayed. After using dos2unix, this went away.

      As for determining your Perl path, use ‘which perl’ to see what your system is loading as a Perl interpreter. The ‘hash’ command is worth looking at too (for bash users).

    • Thanks a lot.. This saved me a lot of time and headache.


    Leave a reply