# Can one capture what a command prints on the REPL in a variable of type String?

**URL:** <https://discourse.julialang.org/t/can-one-capture-what-a-command-prints-on-the-repl-in-a-variable-of-type-string/73999>\
**Category:** General Usage\
**Tags:** question, repl, io\
**Created:** [January 3, 2022, 8:34pm UTC](https://discourse.julialang.org/t/can-one-capture-what-a-command-prints-on-the-repl-in-a-variable-of-type-string/73999 "2022-01-03T20:34:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)\
**Post date:** [January 3, 2022, 8:34pm UTC](https://discourse.julialang.org/t/can-one-capture-what-a-command-prints-on-the-repl-in-a-variable-of-type-string/73999/1 "2022-01-03T20:34:45Z")

</div>

I want to display on a list of the packages I am using or importing. I know this command

```julia
Pkg.status()

```

wll print the list on the screen, but with other items I am not interested in. So I would like to capture what is printed on the screen as a string, clean it up, and then store it in a String variable.

I tried to do this

```julia
captured_string = "$(Pkg.status())"

```

but `captured_string` turned out to be the string “nothing.”

Is there a way to capture what is printed by this command?

Note: the question is not specific to `Pkg.status()`. I would like to be able to capture what is printed on the screen by any Julia command.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [January 3, 2022, 8:44pm UTC](https://discourse.julialang.org/t/can-one-capture-what-a-command-prints-on-the-repl-in-a-variable-of-type-string/73999/2 "2022-01-03T20:44:47Z")

</div>

Try [Suppressor.jl](https://juliahub.com/ui/Packages/Suppressor/1JN4R/0.2.0).

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [January 3, 2022, 8:56pm UTC](https://discourse.julialang.org/t/can-one-capture-what-a-command-prints-on-the-repl-in-a-variable-of-type-string/73999/3 "2022-01-03T20:56:24Z")

</div>

> [@Soldalma](#):
>
> Note: the question is not specific to `Pkg.status()` . I would like to be able to capture what is printed on the screen by any Julia command.

You can do this with `sprint` and any function that takes an I/O stream argument to print into.  
For `Pkg.status`:

```julia
sprint(io->Pkg.status(;io))

```

---

<div class="post-metadata">

**Author:** ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)\
**Post date:** [January 4, 2022, 3:36am UTC](https://discourse.julialang.org/t/can-one-capture-what-a-command-prints-on-the-repl-in-a-variable-of-type-string/73999/4 "2022-01-04T03:36:10Z")

</div>

> [@yha](#):
>
> `sprint(io->Pkg.status(;io))`

Thanks. Exactly what I needed.
