6

I've a simple script:

#!/usr/bin/env perl -w
print "Hello World\n"

Make this executable, run on Linux, and I get:

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

(without the -w, this works OK)

Running the same script on a Solaris 8 machine produces the correct output.

Any suggestions as to why this is ?

Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
Tim T.
  • 63
  • 3

3 Answers3

5

It's not env; it's the kernel's #! handler. Everything after the first word (/usr/bin/env) is passed as a single argument string. Safest/most portable is to not put anything after the perl there.

geekosaur
  • 7,025
  • 1
  • 19
  • 19
0

Which shell are you using on both machines? Also, which version of Solaris and Linux are you using?

It's possible that you're using KSH on Solaris by default and BASH on Linux. This difference may affect how the shebang line is executed.

HTH!

Tom Purl

Tom Purl
  • 549
  • 1
  • 3
  • 13
0

The wikipedia article mentions this portability issues:

http://en.wikipedia.org/wiki/Shebang_%28Unix%29

"Another portability problem is the interpretation of the command arguments. Some systems [12] do not split up the arguments; for example, when running the script with the first line like,..."

I found this a bit surprising, but it is the shell that does the parsing of arguments on spaces, and there isn't a shell involved here.

JOTN
  • 1,727
  • 1
  • 10
  • 12
  • Not *just* the shell. `#!` scripts are handled by the kernel directly (`#!` is actually defined as an executable "magic number"), and the kernel parses the `#!` line and spawns the named program, passing the rest of the line as parameters. On most commercial Unixes, the line length is limited to 40 characters and the entire line after the space following the executable name (if any) is passed as a single parameter. Linux and *BSD kernels do more complete line parsing and the length limit is something like a memory block (4KB). – geekosaur Mar 17 '11 at 00:16
  • One of the easier sources to find of documentation for this is the `perlrun(1)` manpage, where it talks about `#!` emulation and its pitfalls. – geekosaur Mar 17 '11 at 00:18