# Feeding Julia strings in pipe to external program

**URL:** <https://discourse.julialang.org/t/feeding-julia-strings-in-pipe-to-external-program/1333>\
**Category:** General Usage\
**Created:** [January 6, 2017, 8:31pm UTC](https://discourse.julialang.org/t/feeding-julia-strings-in-pipe-to-external-program/1333 "2017-01-06T20:31:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [January 6, 2017, 8:31pm UTC](https://discourse.julialang.org/t/feeding-julia-strings-in-pipe-to-external-program/1333/1 "2017-01-06T20:31:32Z")

</div>

Is it possible to use a string, instead of the output of a command, when using `pipeline`?  
For example instead of writing

```julia
run(pipeline(`echo hello`, `sort`))

```

I would write something like

```julia
run(pipeline("hello", `sort`))

```

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [January 6, 2017, 8:35pm UTC](https://discourse.julialang.org/t/feeding-julia-strings-in-pipe-to-external-program/1333/2 "2017-01-06T20:35:58Z")

</div>

Note that I tried doing

```julia
run(pipeline(`echo $("hello")`, `sort`))

```

which I believe would work but unfortunately `echo` is not available on Windows (despite it being available in cmd)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 6, 2017, 8:42pm UTC](https://discourse.julialang.org/t/feeding-julia-strings-in-pipe-to-external-program/1333/3 "2017-01-06T20:42:07Z")

</div>

You can use `open` to write arbitrary Julia data to a pipeline or other command:

```julia
julia> open(pipeline(`cat`, `sort`), "w", STDOUT) do f
           println(f, "hello")
       end
hello

```

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [January 6, 2017, 8:50pm UTC](https://discourse.julialang.org/t/feeding-julia-strings-in-pipe-to-external-program/1333/4 "2017-01-06T20:50:32Z")

</div>

Thanks! This is still not fully clear to me. If I wanted to feed `"hello"` to the sort command, as above,what would I do, would I still need the `pipeline` function (what is the `cat` for here)?

Also, how would I capture back the output in a string?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 7, 2017, 12:10am UTC](https://discourse.julialang.org/t/feeding-julia-strings-in-pipe-to-external-program/1333/5 "2017-01-07T00:10:44Z")

</div>

The `cat` was only an example. If you just have `sort`, then you don’t need `pipeline` at all, just use

```julia
open(`sort`, "w", STDOUT)

```

If you want to read the output into a string, you can do:

```julia
stdout, stdin, process = readandwrite(`sort`)
println(stdin, "hello")
close(stdin)
readstring(stdout)

```

---

<div class="post-metadata">

**Author:** ![nico202](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nico202/32/1009_2.png) [@nico202](https://discourse.julialang.org/u/nico202)\
**Post date:** [July 16, 2018, 4:35pm UTC](https://discourse.julialang.org/t/feeding-julia-strings-in-pipe-to-external-program/1333/6 "2018-07-16T16:35:41Z")

</div>

How did this became in 0.7?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 16, 2018, 5:59pm UTC](https://discourse.julialang.org/t/feeding-julia-strings-in-pipe-to-external-program/1333/7 "2018-07-16T17:59:55Z")

</div>

> [@nico202](#):
>
> How did this became in 0.7?

Write to external program, which outputs to stdout:

```julia
julia> open(`sort`, "w", stdout) do f
           print(f, "c\nb\na\n")
       end
a
b
c

```

Read and write to external program:

```julia
f = open(`sort`, "r+")
print(f, "c\nb\na\n")
close(f.in)
read(f, String) # returns "a\nb\nc\n"

```

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [September 17, 2022, 6:36pm UTC](https://discourse.julialang.org/t/feeding-julia-strings-in-pipe-to-external-program/1333/8 "2022-09-17T18:36:13Z")

</div>

To follow up on this, you can now wrap the string with an `IOBuffer` and use it as the `stdin` argument to `pipeline`, which lets it be used by any function which takes a command, e.g.

```julia
julia> readlines(pipeline(`sort`,stdin=IOBuffer("c\nb\na\n")))
3-element Vector{String}:
 "a"
 "b"
 "c"

```
