How to invoke bash or shell scripts from a haskell program?

Peter Hall picture Peter Hall · May 10, 2012 · Viewed 9.3k times · Source

I'm writing some shell scripts with haskell, which I'm running in gitbash, but there are a few other existing scripts I'd like to be able to use from those scripts.

For example, I'd like to run maven goals or do a git pull, but without having to integrate specifically with those tools.

Is there a way to do this?

Answer

You can use System.Process. For example, executing seq 1 10 shell command:

> import System.Process

> readProcess "seq" ["1", "10"] ""
"1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n"
it :: String

> readProcessWithExitCode  "seq" ["1", "10"] ""
(ExitSuccess,"1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n","")
it :: (GHC.IO.Exception.ExitCode, String, String)