Run program with string input and output

How do I run a program with a string on its stdin and get its output as a string?

This works but it’s a little verbose.

julia> let s = "hello"
           io = IOBuffer()
           run(`cat`, IOBuffer(s), io)
           seekstart(io)
           read(io, String)
       end
"hello"

Maybe

julia> read(pipeline(IOBuffer("hello"), `rev`), String)
"olleh"
2 Likes

While I know this isn’t the “done thing” with Julia, it would be nice to be able to pun on | and shell pipelines and do "hello" | `rev`.

You can easily do that for yourself:

julia> Base.:|(s::AbstractString, cmd::Cmd) = read(pipeline(IOBuffer(s), cmd), String)
julia> "hello" | `rev`
"olleh"

That being said, it would probably be more Juliaesque to use the |> operator for this.

julia> Base.:|>(s::AbstractString, cmd::Cmd) = read(pipeline(IOBuffer(s), cmd), String)
julia> "hello" |> `rev`
"olleh"