# Create external command from string

**URL:** <https://discourse.julialang.org/t/create-external-command-from-string/17568>\
**Category:** General Usage\
**Created:** [November 15, 2018, 5:37pm UTC](https://discourse.julialang.org/t/create-external-command-from-string/17568 "2018-11-15T17:37:30Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![armstrtw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/armstrtw/32/5978_2.png) [@armstrtw](https://discourse.julialang.org/u/armstrtw)\
**Post date:** [November 15, 2018, 5:37pm UTC](https://discourse.julialang.org/t/create-external-command-from-string/17568/1 "2018-11-15T17:37:30Z")

</div>

are backticks the only way to construct a command for the ‘run’ function:

for example:

> julia\> cmd = “pwd”::String  
> “pwd”
> 
> julia\> run(`$cmd`)  
> /home/warmstrong  
> Process(`pwd`, ProcessExited(0))
> 
> julia\>

what is the ` function?

Is there a way to create a command from a string with a function other than the backtick?

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [November 15, 2018, 5:46pm UTC](https://discourse.julialang.org/t/create-external-command-from-string/17568/2 "2018-11-15T17:46:17Z")

</div>

```julia
julia> Cmd(["ls", "-la"]) == `ls -la`
true

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 15, 2018, 5:49pm UTC](https://discourse.julialang.org/t/create-external-command-from-string/17568/3 "2018-11-15T17:49:48Z")

</div>

> [@armstrtw](#):
>
> Is there a way to create a command from a string with a function other than the backtick?

`@cmd "ls -l"` or `Cmd(["ls", "-l"])` will do what you want (producing the ``ls -l`` command).

---

<div class="post-metadata">

**Author:** ![armstrtw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/armstrtw/32/5978_2.png) [@armstrtw](https://discourse.julialang.org/u/armstrtw)\
**Post date:** [November 15, 2018, 5:51pm UTC](https://discourse.julialang.org/t/create-external-command-from-string/17568/4 "2018-11-15T17:51:08Z")

</div>

thanks. I think ‘Cmd’ is more readable.

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [March 11, 2020, 11:32am UTC](https://discourse.julialang.org/t/create-external-command-from-string/17568/5 "2020-03-11T11:32:24Z")

</div>

Hi. I realise this is quite old, but Cmd() is not recognised (maybe deprecated?) and @cmd doesn’t seem to work very well. Even doing something simple like:  
ls -lt | head  
is something I cannot get to work. First trying  
mycmd = @cmd “ls -lt | head”  
Yields a warning that I should use quotes around the pipe:

mycmd = @cmd “ls -lt ‘|’ head”  
is accepted, but then when I try  
run(mycmd)  
the response is

julia\> run(mycmd)  
ls: head: No such file or directory  
ls: |: No such file or directory  
ERROR: failed process: Process(`ls -lt '|' head`, ProcessExited(1)) [1]  
[The shell seems to not see the pipe and looks for the file ‘head’ which isn’t there.]

Have there been any recent developments in this?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 11, 2020, 2:33pm UTC](https://discourse.julialang.org/t/create-external-command-from-string/17568/6 "2020-03-11T14:33:10Z")

</div>

> [@compleat](#):
>
> I realise this is quite old, but Cmd() is not recognised (maybe deprecated?) and @cmd doesn’t seem to work very well.

Both `Cmd` and `@cmd` still work.

The problem you are having is that `ls -lt | head` is a shell pipeline, but `Cmd` is not a shell — it is a way of launching individual programs. So, what you are doing is calling `ls` with filenames `|` and `head`, which is not what you want.

To construct a pipeline, see the [pipeline documentation in the manual](https://docs.julialang.org/en/latest/manual/running-external-programs/#Pipelines-1):

```julia
run(pipeline(`ls -l`, `head`))

```

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [March 11, 2020, 2:38pm UTC](https://discourse.julialang.org/t/create-external-command-from-string/17568/7 "2020-03-11T14:38:53Z")

</div>

Thanks Steven,

I will use that with @cmd

When I tried Cmd, I got below:

Cmd(“ls”)  
ERROR: MethodError: no method matching Cmd(::String)

[must be something simple?]

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 11, 2020, 3:22pm UTC](https://discourse.julialang.org/t/create-external-command-from-string/17568/8 "2020-03-11T15:22:38Z")

</div>

You need to pass an array of strings to `Cmd`:

```julia
julia> Cmd(["ls"])
`ls`

```

Unfortunately this doesn’t seem to be documented.  
Also, it would seem reasonable to allow a single string here.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 11, 2020, 3:23pm UTC](https://discourse.julialang.org/t/create-external-command-from-string/17568/9 "2020-03-11T15:23:33Z")

</div>

The `Cmd` constructor takes an _array_ of strings, as shown in my examples above.

---

<div class="post-metadata">

**Author:** ![compleat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/compleat/32/8958_2.png) [@compleat](https://discourse.julialang.org/u/compleat)\
**Post date:** [March 11, 2020, 10:15pm UTC](https://discourse.julialang.org/t/create-external-command-from-string/17568/10 "2020-03-11T22:15:21Z")

</div>

Thanks very much for clarifying!
