Chapter 4. Other ways of running Hugs

4.1. Running standalone Haskell programs

runhugs [option...] file [argument...]

The runhugs command is an interpreter for an executable Hugs script. The first non-option should be the name of a file containing a Haskell Main module. The runhugs command will invoke the the main function in this module, with any subsequent arguments available through the getArgs action.

For example, suppose we have a file echo.hs containing

module Main where

import System.Environment

main = do
        args <- getArgs
        putStrLn (unwords args)
Then we can run this program with the command
runhugs echo.hs a b c
We can also test the program from within the interpreter using the withArgs function from the System.Environment module:
Main> withArgs ["a", "b", "c"] main
a b c

On Unix systems, it is possible for an executable file to specify which program is used to run it. To do this we need to make the module a literate script, like the following:

#! /usr/local/bin/runhugs +l

> module Main where

> import System.Environment

> main = do
>       args <- getArgs
>       putStrLn (unwords args)
If this file is called myecho, and is executable, we can say
myecho a b c
This invokes the command
/usr/local/bin/runhugs +l myecho a b c
The +l option tells runhugs that myecho contains a literate script, even though its name does not end in ".lhs".

Note: Unfortunately, the #! feature passes additional arguments (if any) to the program as a single argument: if the first line were

#! /usr/local/bin/runhugs +l -98
then the first argument to runhugs would be "+l -98". You can get around this using the -X option, which asks for the string to be split into options:
#! /usr/local/bin/runhugs -X +l -98
Then the program will read and act on both the +l and -98 options.