2

I have a file named "test.txt" with this text:

Good
Morning
Sir

and a file named "test.hs" with the following code:

module Main where

import System.IO

main :: IO ()
main = interact f

f :: String -> String
f s = head $ lines s

The following command...

cat test.txt | runhaskell test.hs

outputs

Good


However, I would like to explicitly pass parameters to runhaskell without relying on a file, like:

echo "Good\nMorning\nSir" | runhaskell test.hs

and also execute runhaskell with a literal string of Haskell code, like:

echo "Good\nMorning\nSir" | runhaskell "module Main where\nimport System.IO\nmain :: IO ()\nmain = interact f\nf :: String -> String\nf s = head $ lines s"

Is this technically possible ?

1 Answer 1

2

The problem is that echo will output a backslash (\) and an n instead of a new line.

You can use the -e flag [unix.com], this flag will:

-e enable interpretation of backslash escapes

So we can pass a string with new lines to runhaskell's input channel with:

echo -e -- "Good\nMorning\nSir" | runhaskell test.hs

Note that cat test.txt | runhaskell test.hs is useless use of cat, you can replace this with:

runhaskell test.hs < test.txt

which is more efficient since we do not use a cat process or the pipe to pass data to.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much. Why do you have two dashes after -e ?
@F.Zer: that is to prevent that the content of the string, is interpreted as a flag. Here that is not the case, but imagine that the string for example is -n, then echo parses this as a flag. By using two dashes, it is seen as a parameter: unix.stackexchange.com/questions/11376/…
Thank you ! Do you know whether my second concern is technically possible ?

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.