# Assign output of external command to julia variable

**URL:** <https://discourse.julialang.org/t/assign-output-of-external-command-to-julia-variable/23561>\
**Category:** New to Julia\
**Tags:** question, shell\
**Created:** [April 26, 2019, 2:25pm UTC](https://discourse.julialang.org/t/assign-output-of-external-command-to-julia-variable/23561 "2019-04-26T14:25:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [April 26, 2019, 2:25pm UTC](https://discourse.julialang.org/t/assign-output-of-external-command-to-julia-variable/23561/1 "2019-04-26T14:25:19Z")

</div>

Dear all,  
how can I assign the output of a shell pipeline to a julia’s variable?  
Let’s say I have this pipe:

```julia
$ grep 'above' textExample.txt | head -1 | cut -d " " -f 4
blessed
$ x=$(grep 'above' textExample.txt | head -1 | cut -d " " -f 4)
$ echo $x
blessed

```

how do I assign the pipeline’s output `blessed` to a variable like I did with shell? I tried:

```julia
julia> x = readall(run(pipeline(`grep 'above' textExample.txt`),
       `head -1`, `cut -d " " -f 4`))
ERROR: MethodError: no method matching spawn_opts_inherit(::Cmd, ::Cmd)
Stacktrace:
 [1] #run#503(::Bool, ::Function, ::Cmd, ::Cmd, ::Vararg{Cmd,N} where N) at ./process.jl:662
 [2] run(::Cmd, ::Cmd, ::Vararg{Cmd,N} where N) at ./process.jl:661
 [3] top-level scope at none:0

```

Thanks

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [April 26, 2019, 2:40pm UTC](https://discourse.julialang.org/t/assign-output-of-external-command-to-julia-variable/23561/2 "2019-04-26T14:40:49Z")

</div>

You may have a typo in your pipeline declaration. This works for me in Julia v1.1.0:

```
p = pipeline(`grep 'above' textExample.txt`, `head -1`, `cut -d " " -f 4`)
x = read(run(p))

```

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [April 26, 2019, 2:51pm UTC](https://discourse.julialang.org/t/assign-output-of-external-command-to-julia-variable/23561/3 "2019-04-26T14:51:33Z")

</div>

You are using `read` instead of `readall`; is this because it is Julia 1+ wherease the example I have seen was from a previous version?  
Anyway, by copying the text from here to avoid typos, I got:

```julia
julia> x = read(run(p))
blessed
ERROR: MethodError: no method matching read(::RawFD)
Closest candidates are:
  read(::Base.DevNull, ::Type{UInt8}) at coreio.jl:12
  read(::IOStream) at iostream.jl:481
  read(::IOStream, ::Type{UInt8}) at iostream.jl:393
  ...
Stacktrace:
 [1] read(::Base.ProcessChain) at ./io.jl:231
 [2] top-level scope at none:0
julia> x
Process(`ls`, ProcessExited(0))

julia> print(x)

julia> x = readall(run(p))
blessed
ERROR: UndefVarError: readall not defined
Stacktrace:
 [1] top-level scope at none:0
julia> x
Process(`ls`, ProcessExited(0))

```

both with `read` and `readall`, the julia variable does not contain the shell output…

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [April 26, 2019, 2:57pm UTC](https://discourse.julialang.org/t/assign-output-of-external-command-to-julia-variable/23561/4 "2019-04-26T14:57:36Z")

</div>

Anyway, you were right: there is a typo in the command: a `)` too much at the first newline and a `)` less at the very end; anyway, neither `readall` nor `read` are defined methods when I run them:

```julia
julia> x = readall(run(pipeline(`grep 'above' textExample.txt`,
              `head -1`, `cut -d " " -f 4`))
              )
blessed
ERROR: UndefVarError: readall not defined
Stacktrace:
 [1] top-level scope at none:0

blessed
blessed

```

`blessed` comes out though. Is there a package that I need to load?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [April 26, 2019, 3:09pm UTC](https://discourse.julialang.org/t/assign-output-of-external-command-to-julia-variable/23561/5 "2019-04-26T15:09:44Z")

</div>

I don’t think you should mix `read` and `run`. This works for me on Julia 1.0.

```julia
julia> read(pipeline(`date`, `tr " " _`), String)
"fre_26_apr_2019_17:07:42_CEST\n"

```

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [April 26, 2019, 3:13pm UTC](https://discourse.julialang.org/t/assign-output-of-external-command-to-julia-variable/23561/6 "2019-04-26T15:13:16Z")

</div>

Yep: you are right!

```julia
julia> x = read(pipeline(`grep 'above' textExample.txt`,
              `head -1`, `cut -d " " -f 4`), String)
"blessed\n"

```

I just need to remove the trailing newline. Thank you.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [April 26, 2019, 3:25pm UTC](https://discourse.julialang.org/t/assign-output-of-external-command-to-julia-variable/23561/7 "2019-04-26T15:25:28Z")

</div>

> [@Luigi\_Marongiu](#):
>
> I just need to remove the trailing newline. Thank you.

```julia
julia> readchomp(pipeline(`date`, `tr " " _`))
"fre_26_apr_2019_17:08:33_CEST"

```
