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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With