Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use perl's switches with /bin/env in the shebang line?

I want to run perl -w using env. That works fine on the command line:

$ /bin/env perl -we 'print "Hello, world!\n"'
Hello, world!

But it doesn't work on the shebang line in a script:

#!/bin/env perl -w
print "Hello, world!\n";

Here is the error:

/bin/env: perl -w: No such file or directory

Apparently env doesn't understand the -w flag that I'm passing to perl. What's wrong?

like image 868
Frank Avatar asked Feb 20 '09 16:02

Frank


People also ask

What is passing the usr bin env for in the shebang?

As we mentioned earlier,#!/usr/bin/env bash is also a shebang line used in script files to execute commands with the Bash shell. It uses the env command to display the environment variables present in the system and then execute commands with the defined interpreter.

What is the use of shebang line in perl?

This line instructs the operating system running the Perl script on where the executable for Perl and its associated files are located. This line is commonly only required in Linux and Unix variants, users running Perl in Microsoft Windows do not need this line.

Does shebang need first line?

The shebang must be the first line because it is interpreted by the kernel, which looks at the two bytes at the start of an executable file. If these are #! the rest of the line is interpreted as the executable to run and with the script file available to that program.

What does usr bin env python3 mean?

A shebang line #!/usr/bin/python3Its purpose is to define the location of the interpreter. By adding the line #!/usr/bin/python3 on the top of the script, we can run the file.py on a Unix system and automatically will understand that this is a python script. Alternative, you could run the script as python3 file.py .


3 Answers

The hash-bang isn't a normal shell command-line, the parsing and white-space handling is different - that's what you've hit. See:

  • http://www.in-ulm.de/~mascheck/various/shebang/
  • http://homepages.cwi.nl/~aeb/std/hashexclam-1.html#ss1.3
  • http://en.wikipedia.org/wiki/Shebang_(Unix)

Basically many/most unixes put all of the remaining text after the first space into a single argument.

So:

#!/bin/env perl -w

is the equivalent of:

/bin/env "perl -w"

so you need to handle any options to the perl interpreter in some other fashion. i.e.

use warnings;

(as @Telemachus)

like image 108
Douglas Leeder Avatar answered Sep 26 '22 22:09

Douglas Leeder


Instead of -w use the warnings pragma (for modern versions of Perl):

#!/bin/env perl
use warnings;
use strict;
like image 16
Telemachus Avatar answered Sep 26 '22 22:09

Telemachus


I thought it might be useful to bring up that "-w" is not the same as "use warnings". -w will apply to all packages that you use, "use warnings" will only apply lexically. You typically do not want to use or rely upon "-w"

like image 5
vhold Avatar answered Sep 25 '22 22:09

vhold