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

**URL:** <https://discourse.julialang.org/t/easier-way-to-feed-stdin-to-external-program-and-fetch-stdout/48503>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 16, 2020, 5:02pm UTC](https://discourse.julialang.org/t/easier-way-to-feed-stdin-to-external-program-and-fetch-stdout/48503 "2020-10-16T17:02:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![matklad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matklad/32/17976_2.png) [@matklad](https://discourse.julialang.org/u/matklad)\
**Post date:** [October 16, 2020, 5:02pm UTC](https://discourse.julialang.org/t/easier-way-to-feed-stdin-to-external-program-and-fetch-stdout/48503/1 "2020-10-16T17:02:12Z")

</div>

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](https://docs.julialang.org/en/v1/manual/running-external-programs/#Avoiding-Deadlock-in-Pipelines).

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?

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [October 16, 2020, 5:17pm UTC](https://discourse.julialang.org/t/easier-way-to-feed-stdin-to-external-program-and-fetch-stdout/48503/2 "2020-10-16T17:17:09Z")

</div>

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
julia> sprint() do out_stream
           open(`cat`, "w", out_stream) do in_stream
               println(in_stream, "Hello world!")
           end
       end
"Hello world!\n"

```
