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?
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.
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.
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.
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 .
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:
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)
Instead of -w
use the warnings pragma (for modern versions of Perl):
#!/bin/env perl
use warnings;
use strict;
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"
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