2

How do I feed a string to shell command and get the output it produces in Haskell?

For example, given this:

> myHaskellProg 
  "blah"

> myHaskellProg | wc
  4

I want

> myHaskellProg

to print 4 by calling wc and printing out the result

I was able to call shell commands using process's callCommand but how do I pipe a string to it and get back the result or stderr?

1 Answer 1

5

You are looking for System.Process and you can use the shell function to create a description of a process then use readCreateProcess to run the shell command, provide stdin and read stdout.

import System.Process
main :: IO ()
main =
  do result <- readCreateProcess (shell "ls") myHaskellString
     putStrLn result

myHaskellString :: String
myHaskellString = "string"
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please show me how to print stderr produced from the process?
Use readCreateProcessWithExitCode to capture both the return value and stderr.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.