[Haskell-beginners] Cross-platform .hs files on Linux and Windows

Vinay Sajip vinay_sajip at yahoo.co.uk
Tue Apr 17 13:06:45 CEST 2012

Marius Ghita <mhitza <at> gmail.com> writes:

> http://en.wikipedia.org/wiki/Shebang_%28Unix%29when it occurs as the first two
characters on the first line of a text file. In this case, the program loader in
Unix-like operating systems parses the rest of the first line as an interpreter
>  and invokes the program specified after the character sequence with any
>  command line options specified as parameters. The name of the file 
> being executed is passed as the final argument.
> As you can see this is not something an interpreter is supposed to do, the
> responsability is delegated to the OS's program loader. You could emulate
> this under Windows, but I suppose you would be using cygwin or something
> similar.

Yes, but on Linux, if you have an executable script hello.hs like this:

#!/usr/bin/env runhaskell
main = putStrLn "Hello, world!"

then when you invoke it as 

$ ./hello.hs

the shell opens the file and looks for a shebang line, finds it, interprets it,
and then calls e.g.

/usr/bin/runhaskell hello.hs

which then parses that file, skipping the shebang line, and produces the
expected result.

On Windows, even if we arrange for the same thing to be done at the OS level,
the operation would fail, because the Windows version of runhaskell does not
skip the shebang line, but tries to parse it as Haskell source.

I'm not saying runhaskell needs to do anything more than skip a shebang line, on
Windows, if it sees one. Otherwise, you can't have cross-platform scripts on
Windows and Linux.


Vinay Sajip

More information about the Beginners mailing list