# Piping directly to STDIN

**URL:** <https://discourse.julialang.org/t/piping-directly-to-stdin/2446>\
**Category:** General Usage\
**Created:** [March 4, 2017, 5:31pm UTC](https://discourse.julialang.org/t/piping-directly-to-stdin/2446 "2017-03-04T17:31:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [March 4, 2017, 5:31pm UTC](https://discourse.julialang.org/t/piping-directly-to-stdin/2446/1 "2017-03-04T17:31:23Z")

</div>

I’m writing an integration test and want to know the best way to pipe to `STDIN`.

The closest I’ve gotten is `print(STDIN, "blah")`, but this still required me to press enter.

What’s the best way to accomplish this?

I know `redirect_stdin()` exists, but the docs and googlable posts don’t really say much.

---

<div class="post-metadata">

**Author:** ![kevin.squire](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevin.squire/32/62_2.png) [@kevin.squire](https://discourse.julialang.org/u/kevin.squire)\
**Post date:** [March 4, 2017, 6:54pm UTC](https://discourse.julialang.org/t/piping-directly-to-stdin/2446/2 "2017-03-04T18:54:44Z")

</div>

Not necessarily the best solution, but did you try adding `\n` to the print statement?

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [March 4, 2017, 7:26pm UTC](https://discourse.julialang.org/t/piping-directly-to-stdin/2446/3 "2017-03-04T19:26:48Z")

</div>

That didn’t work. 😢

The problem is I have to type enter before anything gets added to the buffer

* * *

edit: for reference, here’s the thing that’s waiting for input

```julia
function user_input(prompt)
  print(prompt)
  answer = readline()
  chomp(answer)
end

```

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [March 4, 2017, 9:40pm UTC](https://discourse.julialang.org/t/piping-directly-to-stdin/2446/4 "2017-03-04T21:40:40Z")

</div>

Just guessing here, but maybe this could help:  
[http://docs.julialang.org/en/stable/stdlib/io-network/#Base.flush](http://docs.julialang.org/en/stable/stdlib/io-network/#Base.flush)

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [March 4, 2017, 9:58pm UTC](https://discourse.julialang.org/t/piping-directly-to-stdin/2446/5 "2017-03-04T21:58:58Z")

</div>

Is this something like what you want?

```julia
let filename = tempname()
    open(filename,"w") do f
        println(f,"my answer!")
    end
    ret = open(filename) do f
        redirect_stdin(f) do
            user_input("what do you say?")
        end
    end
   
    println("\nresult was: ",ret) # for testing the test

    rm(filename)
    contains(ret, "my answer!") || error("test failed!")
end

```

(cribbed from the Julia test suite)

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [March 4, 2017, 10:26pm UTC](https://discourse.julialang.org/t/piping-directly-to-stdin/2446/6 "2017-03-04T22:26:29Z")

</div>

Bam. Solved.

Thanks man!
