# Construct Shell Command and Run in Function

**URL:** https://discourse.julialang.org/t/construct-shell-command-and-run-in-function/24504
**Category:** New to Julia
**Created:** [May 23, 2019, 7:49am UTC](https://discourse.julialang.org/t/construct-shell-command-and-run-in-function/24504 "2019-05-23T07:49:55Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Donut\_Meepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donut_meepo/32/9085_2.png) [@Donut\_Meepo](https://discourse.julialang.org/u/Donut_Meepo)
#### Post date: [May 23, 2019, 7:49am UTC](https://discourse.julialang.org/t/construct-shell-command-and-run-in-function/24504/1 "2019-05-23T07:49:55Z")

</div>

I am trying to construct a command out of the function arguments and run it. Here is an failed example

```julia
function display_file(fn, method)
    cmd = join([method, fn], " ")
    run(`$cmd`)
end

```

It seems you cannot run the whole command from interpolation

```julia
display_file("test.txt", "cat")

```

ERROR: IOError: could not spawn `‘cat test.txt’`: no such file or directory (ENOENT).

I think it is because it requires the command to be not quoted? Like, `cat ‘test.txt’` is valid.

I tried to convert the string to the cmd object by macro @cmd.

```julia
function display_file(fn, method)
    cmd = join([method, fn], " ")
    cmd = @cmd cmd
    run(`$cmd`)
end

```

It didn’t let me make the function

ERROR: LoadError: MethodError: no method matching shell\_parse(::Symbol; special=“#{}()\<\>|&\*?~;”)

I am not very sure how the macro works. Thank you for your help.

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [May 23, 2019, 8:12am UTC](https://discourse.julialang.org/t/construct-shell-command-and-run-in-function/24504/2 "2019-05-23T08:12:47Z")

</div>

`join` creates a string, and when you interpolate that into the cmd it will be quoted (as indicated by the error, note the single quotes in

```julia
could not spawn `'cat test.txt'`

```

Why do you have to use `join` at all? This works:

```julia
function display_file(fn, method)
    run(`$method $fn`)
end

```

---

<div class="post-metadata">

### Author: ![Donut\_Meepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donut_meepo/32/9085_2.png) [@Donut\_Meepo](https://discourse.julialang.org/u/Donut_Meepo)
#### Post date: [May 23, 2019, 8:23am UTC](https://discourse.julialang.org/t/construct-shell-command-and-run-in-function/24504/3 "2019-05-23T08:23:38Z")

</div>

Thanks for the reply.

> [@fredrikekre](#):
>
> function display\_file(fn, method) run(`$method $fn`) end

The one you suggested works on the “cat” command. Originally I was trying to do the following

```julia
display_file("https://juliacomputing.com/docs/JuliaProQuickStartGuideLinux_1.1.0.1.pdf", "wget")

```

It can start downloading without issues. But when I add parameters for wget

```julia
display_file("https://juliacomputing.com/docs/JuliaProQuickStartGuideLinux_1.1.0.1.pdf", "wget -np -nH")

```

It returns error

```julia
ERROR: IOError: could not spawn `'wget -np -nH' https://juliacomputing.com/docs/JuliaProQuickStartGuideLinux_1.1.0.1.pdf`: no such file or directory (ENOENT)

```

What should be the correct way to do it?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [May 23, 2019, 8:29am UTC](https://discourse.julialang.org/t/construct-shell-command-and-run-in-function/24504/4 "2019-05-23T08:29:04Z")

</div>

> [@Donut\_Meepo](#):
>
> “wget -np -nH”

It’s again the same issue; you input a `String` which will be quoted when you interpolate that in a cmd. Instead you can input a cmd, and interpolate that, e.g. this should work:

```julia
display_file("https://www.....pdf", `wget -np -nH`)

```

---

<div class="post-metadata">

### Author: ![Donut\_Meepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donut_meepo/32/9085_2.png) [@Donut\_Meepo](https://discourse.julialang.org/u/Donut_Meepo)
#### Post date: [May 23, 2019, 8:36am UTC](https://discourse.julialang.org/t/construct-shell-command-and-run-in-function/24504/5 "2019-05-23T08:36:58Z")

</div>

Thanks a lot. That works nicely.

Just want to know a bit more to understand

```julia
display_file("https://www.....pdf", "wget")

```

Why using String “wget”, it can correctly interpolate as command?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [May 23, 2019, 8:42am UTC](https://discourse.julialang.org/t/construct-shell-command-and-run-in-function/24504/6 "2019-05-23T08:42:15Z")

</div>

Because there is nothing in the string `"wget"` that needs to be guarded with a quote, e.g. no spaces in this case.
