4

I've got a perl script which contains the first line as follows:

#!/usr/bin/env perl

I already read that this is executed to find perl in the environment. But now, when I execute this on the command line /usr/bin/env perl, perl itself gets executed (which is located at /usr/bin/perl). But when I start the script using ./myscript.pl, it shows the following error:

/usr/bin/env: No such file or directory

This results out of the fact that it can't find perl, because this error also appears when I type /usr/bin/env xxxxxx.

Can somebody explain me what exactly is the difference when I run it on command line or in a script?

marian0
  • 664
  • 6
  • 15
Florian Müller
  • 7,448
  • 25
  • 78
  • 120

2 Answers2

11

The most likely cause is that you somehow got DOS line endings (CRLF) in myscript.pl. This causes env to search for a file named perl^M (where ^M represents a CR character), and you don't have one.

cjm
  • 61,471
  • 9
  • 126
  • 175
2

Answering the question:

Can somebody explain me what exactly is the difference when I run it on command line or in a script?

#!/usr/bin/env perl

This is bascially telling the OS (that understands the shebang) to find the first "perl" executable in the list of $PATH, and exec that program by appending the current file name after the shebang. It doesn't invokes "perl" directly, like this shebang you'll find in most perl programs:

#!/usr/bin/perl

This is because most UNIX-like systems (especially Linux) comes with perl installed at /usr/bin/perl. Some UNIX does not have perl by default, but can be easily installed latter on. Just that they might end up being at /usr/local/bin/perl. Usually perl programmers call them "system perl".

The first will take the fisrt perl in your $PATH and the second will take the one that ships with your system.

If you don't specify the full path on the shell then it also takes whatever is first in your $PATH.

Alan
  • 1,479
  • 3
  • 20
  • 36
  • 2
    The OP already knows what `/usr/bin/env` does. They're asking why they get an error when they use it in their script. This doesn't answer their question. – ThisSuitIsBlackNot Jan 14 '16 at 15:06
  • There are actually two questions: One that asks if somebody can explain what exactly is the difference when running it on command line or in a script and the other being the reason why his script is not running. – Alan Jan 14 '16 at 15:13
  • Doesn't that explain the difference between command line and script call to Perl? – Alan Jan 14 '16 at 15:27
  • I haven't sorted his script problem, but I definitely tried to be helpful by answering one of his questions. Down voting for that? – Alan Jan 14 '16 at 15:30
  • I downvoted because you didn't answer the question. You explain the difference between `/usr/bin/perl` and `/usr/bin/env perl`. The OP needs to know why `/usr/bin/env perl` works on the command line but fails when used in the shebang. – ThisSuitIsBlackNot Jan 14 '16 at 15:30
  • His words: Can somebody explain me what exactly is the difference when I run it on command line or in a script? – Alan Jan 14 '16 at 15:31
  • In other words, "why do I get the error '/usr/bin/env: No such file or directory' when I run my script, but not when I run `/usr/bin/env perl` on the command line?" See [cjm's answer](http://stackoverflow.com/a/34793290/176646), which actually addresses the OP's problem. – ThisSuitIsBlackNot Jan 14 '16 at 15:32
  • 1
    The OP's problem are actually two problems, one might be line ending due to Windows to Linux file transfer, the other is understanding the difference between calling perl through script and command line. Why do I get down voted because I tried to be helpful with the first problem? Wasn't that a valid question as well? – Alan Jan 14 '16 at 15:37
  • 1
    When answering SO questions that are unclear, or that contain multiple questions or both implicit and explicit questions, a useful strategy might be to re-state the question you're answering (concisely!) in a quote (`>`) at the top of your answer, and be clear about what parts you're *not* answering. If you fail to answer all questions in a question, you'll probably not get a checkmark. But you may get upvoted for the parts that you answer correctly. (In this case, though, you did answer the only actual question in the question. +1 for that.) – ghoti Jan 14 '16 at 15:57
  • Thanks for the explanation @ghoti, will do as advised. – Alan Jan 14 '16 at 16:01