Easier way to feed stdin to external program and fetch stdout?

Let’s say I have n external program which reads input from stdin and writes it to stdout.

What would be the easiest way to feed the given string to the program and get the result back? I think I can open the cmd for reading and writing, and then spawn two tasks, as documented in Running External Programs · The Julia Language.

But this seems very low-level. I wonder if there’s an easier API which lets me specify stdin contents and get stdout contents back with one call, which would manage required concurrency internally?

You can use open on a command and pass your own IO object for open to use as stdout. sprint() do io then collects everything written into io as a string.

julia> sprint() do out_stream
           open(`cat`, "w", out_stream) do in_stream
               println(in_stream, "Hello world!")
           end
       end
"Hello world!\n"
2 Likes