# Create a Cmd from a String

**URL:** <https://discourse.julialang.org/t/create-a-cmd-from-a-string/2842>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 23, 2017, 7:57pm UTC](https://discourse.julialang.org/t/create-a-cmd-from-a-string/2842 "2017-03-23T19:57:15Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [March 23, 2017, 7:57pm UTC](https://discourse.julialang.org/t/create-a-cmd-from-a-string/2842/1 "2017-03-23T19:57:15Z")

</div>

I am using Julia to combine information from a combination of sources and generate a (rather lengthy) string that is a call to an external program. I can generate a `String` containing the text that I would type into the shell but I can’t work out how to turn that into a `Cmd` to call with `run`. I currently get around that by writing the string to a file named `cmd.sh` and calling

```julia
run(`/bin/bash cmd.sh`)

```

but I am convinced there has to be a better way.

I tried

```julia
run(Cmd([mystring;]))

```

but that seemed to generate extra quotes.

Can someone suggest how I can do this?

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [March 23, 2017, 8:15pm UTC](https://discourse.julialang.org/t/create-a-cmd-from-a-string/2842/2 "2017-03-23T20:15:37Z")

</div>

If you need to run it in the shell then `run(`sh -c $cmd`)` is best (where cmd is a String variable)

If you just need to pass multiple arguments, then call just `run(cmd)`, where `cmd` is built up from interpolating other `Cmd` objects and arrays of Strings.

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [March 24, 2017, 4:06pm UTC](https://discourse.julialang.org/t/create-a-cmd-from-a-string/2842/3 "2017-03-24T16:06:29Z")

</div>

Unfortunately the first version doesn’t work because the contents of `$cmd` are enclosed in double quotes. To the shell, it looks like one long file name.

I want to run

```nohighlight
myprog --a=optiona --b=="option b" file1 file2

```

not

```nohighlight
"myprog --a=optiona --b==\"option b\" file1 file2"

```

It seems that I will need to stay with the current approach or try to use your second approach and right now I think writing to a file then executing that as a shell script is easier.

I appreciate that appropriately quoting strings that are parts of commands is important but not having a way to override the quoting is awkward.

---

<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 24, 2017, 4:22pm UTC](https://discourse.julialang.org/t/create-a-cmd-from-a-string/2842/4 "2017-03-24T16:22:03Z")

</div>

> [@dmbates](#):
>
> `myprog --a=optiona --b=="option b" file1 file2`

The right way to do this is to avoid the shell completely. e.g. suppose you have an array `filenames` of filenames strings and an array `options` with the option strings. You would construct the command to run by, for example:

```julia
options = ["--a=optiona", "--b=option b"]
filenames = ["file1", "file2"]
`myprog $options $filenames`

```

Note that in the `options` variable, there is no need for additional quoting. You write `"--b=option b"`, _not_ `"--b=\"option b\""`. The reason is that these options are passed _directly_ to `myprog`, without going through the shell.

Avoiding the whole issue of quoting and escapes is a huge benefit of circumventing the shell, along with the performance advantage of not launching the shell process.

---

<div class="post-metadata">

**Author:** ![dmbates](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmbates/32/44_2.png) [@dmbates](https://discourse.julialang.org/u/dmbates)\
**Post date:** [March 24, 2017, 5:18pm UTC](https://discourse.julialang.org/t/create-a-cmd-from-a-string/2842/5 "2017-03-24T17:18:48Z")

</div>

Thank you, @stevengj. I’ll let that be a lesson to myself to always give an example of what I am trying to accomplish.

It is all working well now.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [March 25, 2017, 2:47am UTC](https://discourse.julialang.org/t/create-a-cmd-from-a-string/2842/6 "2017-03-25T02:47:56Z")

</div>

Also note that writing a `command literal` (inside backticks) will also work.

Here cmd1 and cmd2 are equivalent:

```julia
cmd1 = `myprog --a=optiona --b="option b" file1 file2`

```

```julia
options = ["--a=optiona", "--b=option b"]
filenames = ["file1", "file2"]
cmd2 = `myprog $options $filenames`

```

---

<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 25, 2023, 8:22pm UTC](https://discourse.julialang.org/t/create-a-cmd-from-a-string/2842/7 "2023-03-25T20:22:18Z")

</div>

2 posts were split to a new topic: [Read a command as a string](https://discourse.julialang.org/t/read-a-command-as-a-string/96608)

---

<div class="post-metadata">

**Author:** ![Bittu\_Kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bittu_kumar/32/48032_2.png) [@Bittu\_Kumar](https://discourse.julialang.org/u/Bittu_Kumar)\
**Post date:** [March 25, 2023, 8:15pm UTC](https://discourse.julialang.org/t/create-a-cmd-from-a-string/2842/9 "2023-03-25T20:15:18Z")

</div>

Simply create an array with the command and its arguments, then pass this array to the command object and run the command using run() function.

```julia
x = ["ls", "-la"]
cmd = `$x`
run(cmd)

```

if you want to run a string as a command, then first split the string and collect it and assign the ‘x’ (above) to the splitted array:

```julia
str = "ls -la"
x = split(str)
cmd = `$x`
run(cmd)

```

It works 100%

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [July 19, 2018, 2:48pm UTC](https://discourse.julialang.org/t/create-a-cmd-from-a-string/2842/10 "2018-07-19T14:48:19Z")

</div>

Is there a simple way to run a command from a string and get the output as a string ? Something like

```julia
cmd = "ls *"
out = readstring(`$cmd`)

```

but that works. I managed to get something working by copying and modifying `Base.repl_cmd` but it’s quite ugly and complicated. And I don’t want to do any interpolation, just run the string and get the output.

---

<div class="post-metadata">

**Author:** ![innerlee](https://avatars.discourse-cdn.com/v4/letter/i/b19c9b/32.png) [@innerlee](https://discourse.julialang.org/u/innerlee)\
**Post date:** [July 19, 2018, 3:39pm UTC](https://discourse.julialang.org/t/create-a-cmd-from-a-string/2842/11 "2018-07-19T15:39:02Z")

</div>

> **[GitHub - innerlee/Shell.jl: Run shell commands becomes easy in Julia!](https://github.com/innerlee/Shell.jl)**
>
> Run shell commands becomes easy in Julia! Contribute to innerlee/Shell.jl development by creating an account on GitHub.

It need some upgrade work, though.

---

<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 25, 2023, 8:26pm UTC](https://discourse.julialang.org/t/create-a-cmd-from-a-string/2842/12 "2023-03-25T20:26:28Z")

</div>

To read the output of an an external program as a string, simply do e.g.:

```julia
read(`ls`, String)

```

If by “command” you mean “shell command”, e.g. `"ls *"` is a shell command if you want `*` to be interpreted as a glob metacharacter, then you need to run it with a shell, e.g.:

```julia
shellcmd = "ls *"
read(`sh -c $shellcmd`, String)

```

By default, commands don’t use the shell because [shelling out sucks](https://julialang.org/blog/2012/03/shelling-out-sucks/) — they just directly launch executables. Note also that there are portable, native Julia equivalents for anything you might want to do via a shell, e.g. globbing via [Glob.jl](https://github.com/vtjnash/Glob.jl) or `ls` via [`readdir`](https://docs.julialang.org/en/v1/base/file/#Base.Filesystem.readdir).

PS. Sorry, didn’t mean to necro-post. This came up in my discourse feed because of @Bittu_Kumar’s post above and I didn’t notice that I was responding to one of the ancient posts!

---

<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:** [April 20, 2023, 3:04pm UTC](https://discourse.julialang.org/t/create-a-cmd-from-a-string/2842/13 "2023-04-20T15:04:53Z")

</div>

A post was split to a new topic: [Interpolating multiple parameters into a Cmd](https://discourse.julialang.org/t/interpolating-multiple-parameters-into-a-cmd/97709)
