# How to redirect to "nothing" the standard output of a function I call?

**URL:** <https://discourse.julialang.org/t/how-to-redirect-to-nothing-the-standard-output-of-a-function-i-call/49106>\
**Category:** New to Julia\
**Created:** [October 27, 2020, 9:31am UTC](https://discourse.julialang.org/t/how-to-redirect-to-nothing-the-standard-output-of-a-function-i-call/49106 "2020-10-27T09:31:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [October 27, 2020, 9:31am UTC](https://discourse.julialang.org/t/how-to-redirect-to-nothing-the-standard-output-of-a-function-i-call/49106/1 "2020-10-27T09:31:30Z")

</div>

I have a function that prints some stuff I don’t need (the IPOPT licence… the problem is that this is in a long loop…).

How do I specify, when I call function `foo()` that I want its standard output stream to be redirected to /dev/null or whatever ?

EDIT:  
I did found this, however I don’t know if it is portable, i.e. if it works only in Linux/Mac or also in Windows:

```julia
oldstd = stdout
redirect_stdout(open("/dev/null", "w"))
foo()
redirect_stdout(oldstd) # recover original stdout

```

if `foo` doesn’t have arguments one can use also `redirect_stdout(foo, open("/dev/null", "w"))`

EDIT2:  
I [did found](https://github.com/JuliaLang/julia/issues/22879#issuecomment-444644456) that also `redirect_stdout(open("nul", "w"))` works on linux, and as it is claimed that it works on Windows, this should be an OS independent way to go to suppress output, right ?

---

<div class="post-metadata">

**Author:** ![MrJimohAnderson](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@MrJimohAnderson](https://discourse.julialang.org/u/MrJimohAnderson)\
**Post date:** [September 26, 2021, 11:49pm UTC](https://discourse.julialang.org/t/how-to-redirect-to-nothing-the-standard-output-of-a-function-i-call/49106/2 "2021-09-26T23:49:23Z")

</div>

Maybe somthing like this?!

```julia
function ohne_print(print_function::Expr) 
    open("nul","w") do muell
        oldstd = stdout;
        redirect_stdout(muell);
        eval(print_function);
        redirect_stdout(oldstd);
    end
    return
end

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 27, 2021, 12:22am UTC](https://discourse.julialang.org/t/how-to-redirect-to-nothing-the-standard-output-of-a-function-i-call/49106/3 "2021-09-27T00:22:40Z")

</div>

```julia
help?> devnull
search: devnull

  devnull

  Used in a stream redirect to discard all data written to it. Essentially equivalent to /dev/null on Unix or NUL on Windows.
  Usage:

```

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [September 27, 2021, 12:26am UTC](https://discourse.julialang.org/t/how-to-redirect-to-nothing-the-standard-output-of-a-function-i-call/49106/4 "2021-09-27T00:26:09Z")

</div>

Putting the last two answers together more concisely…

```julia
redirect_stdout(devnull) do ... end

```

**EDIT** : Should have tested it, it does not work ☹. This does:

```julia
redirect_stdout(open(tempname(), "w")) do; println(1) end

```

although it’s clearly suboptimal. I’d love to get an answer to [this question](https://discourse.julialang.org/t/how-to-convert-an-iobuffer-to-an-iostream/11801)…

---

<div class="post-metadata">

**Author:** ![lostella](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lostella/32/356_2.png) [@lostella](https://discourse.julialang.org/u/lostella)\
**Post date:** [September 27, 2021, 3:09am UTC](https://discourse.julialang.org/t/how-to-redirect-to-nothing-the-standard-output-of-a-function-i-call/49106/5 "2021-09-27T03:09:54Z")

</div>

Good to know! Link to the docs for completeness: [I/O and Network · The Julia Language](https://docs.julialang.org/en/v1/base/io-network/#Base.redirect_stdout-Tuple%7BFunction,%20Any%7D)

One thing which is not clear from the docs is: what happens to return value of `f`? Hopefully this is returned by `redirect_stdout`
