Search code examples
haskellhaskell-turtle

How can I capture stdout and stderr output from a process with Haskell turtle?


{-# LANGUAGE OverloadedStrings #-}

import Turtle

runSh :: Text -> IO ()
runSh x = view $ inshell x empty

main :: IO ()
main = do
  runSh "echo 'abcxyz'"

I've got the above program which outputs:

Line "abcxyz"

Is there a way I can capture this text without being output to the shell's stdout?

Essentially, I'd like to run the process and get a [Line] value rather then a () value from the IO action.


Solution

  • This is possible with shellStrictWithErr.

    {-# LANGUAGE OverloadedStrings #-}
    
    import Turtle (empty, shellStrictWithErr, ExitCode)
    import Data.Text (Text)
    
    runSh :: Text -> IO (ExitCode, Text, Text)
    runSh x' = shellStrictWithErr x' empty
    
    main :: IO ()
    main = do
      (e, v, v') <- runSh "ls"
      print e
      print v
      print v'