Home » Linux » Can Haskell be used to write shell scripts?

Can Haskell be used to write shell scripts?

Posted by: admin November 30, 2017 Leave a comment

Questions:

Is it possible to write shell scripts in Haskell and if so, how do you do it? Just changing the interpreter like so?

#!/bin/ghci
Answers:

Using ghci will just load the module in GHCi. To run it as a script, use runhaskell or runghc:

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

Questions:
Answers:

Well check this presentation : Practical Haskell: scripting with types

Questions:
Answers:

It should work if you change the interpreter to this:

#!/usr/bin/runhaskell

Questions:
Answers:

As of October 2016, there is a better answer to this question: use The Haskell Tool Stack script interpreter. stack-based Haskell scripts are portable because they download (and cache) the correct version of ghc and all of their package dependencies.

#!/usr/bin/env stack
-- stack --resolver lts-3.2 --install-ghc runghc
main = putStrLn "Hello World!"

See also the tutorial How to Script with Stack.

Questions:
Answers:

Another way to write shell scripts using Haskell is to generate the scripts, such as with bashkell. This is useful if you might want to run on systems that do not have haskell installed.