# How to pass arguments to Cmd?

**URL:** <https://discourse.julialang.org/t/how-to-pass-arguments-to-cmd/73959>\
**Category:** General Usage\
**Tags:** shell, cmd\
**Created:** [January 3, 2022, 11:47am UTC](https://discourse.julialang.org/t/how-to-pass-arguments-to-cmd/73959 "2022-01-03T11:47:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![melonedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/melonedo/32/15503_2.png) [@melonedo](https://discourse.julialang.org/u/melonedo)\
**Post date:** [January 3, 2022, 11:47am UTC](https://discourse.julialang.org/t/how-to-pass-arguments-to-cmd/73959/1 "2022-01-03T11:47:49Z")

</div>

For example I might call ``echo`` with argument `"hello"`, how can I do it? Assume `cmd = `eho` `, I found that `push!(cmd.exec, "hello"` does the trick, but `exec` is not documented, is this recommended?

Also, `run` from Base has the signature `run(command, args...; wait::Bool = true)`. `args` is not documented, what does it mean? I tried `String`s and `Cmd`s but they do not work.

---

<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:** [January 3, 2022, 12:17pm UTC](https://discourse.julialang.org/t/how-to-pass-arguments-to-cmd/73959/2 "2022-01-03T12:17:08Z")

</div>

If you have a literal argument, just do

```julia
run(`echo hello`)

```

If you have it in a variable you would typically interpolate the string into the command

```julia
s = "hello"
run(`echo $s`)

```

There are also options like

```julia
run(Cmd(["echo", "hello"]))

```

---

<div class="post-metadata">

**Author:** ![melonedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/melonedo/32/15503_2.png) [@melonedo](https://discourse.julialang.org/u/melonedo)\
**Post date:** [January 3, 2022, 12:26pm UTC](https://discourse.julialang.org/t/how-to-pass-arguments-to-cmd/73959/3 "2022-01-03T12:26:28Z")

</div>

Thanks, the situation is that I have optional trailing arguments, which can not be added with interpolation:

```julia
s = ""
`cmake -S src $s` # => `cmake -B src ''` instead of `cmake -S src`

```

---

<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:** [January 3, 2022, 3:54pm UTC](https://discourse.julialang.org/t/how-to-pass-arguments-to-cmd/73959/4 "2022-01-03T15:54:57Z")

</div>

> [@melonedo](#):
>
> Thanks, the situation is that I have optional trailing arguments, which can not be added with interpolation:

Just interpolate an array (which can be empty) of your trailing arguments (if any):

```julia
julia> args = [];

julia> `cmake -S src $args`
`cmake -S src`

julia> args = ["foo", "bar baz"];

julia> `cmake -S src $args`
`cmake -S src foo 'bar baz'`

```

You have to to realize that, under the hood, a `Cmd` object is _not_ constucting a single string to execute with a shell. It is constructing an executable name and a list of arguments that are passed to that executable _directly_ in its low-level argument list. So, if `s = ""` and you interpolate `$s`, you are asking Julia to pass a literal empty string as an argument. (This is a good thing! Passing arguments directly means that you don’t have to worry about [unexpected effects](https://julialang.org/blog/2012/03/shelling-out-sucks/#metacharacter_brittleness) from spaces or metacharacters in the string.)

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 3, 2022, 4:33pm UTC](https://discourse.julialang.org/t/how-to-pass-arguments-to-cmd/73959/5 "2022-01-03T16:33:58Z")

</div>

You can also splice commands into each other and it works they way you’d want, which is by treating `Cmd` objects like funny arrays of words. Example:

```julia
cmd = `ls -l`
if color
    cmd = `$cmd --color=yes`
end

```

---

<div class="post-metadata">

**Author:** ![melonedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/melonedo/32/15503_2.png) [@melonedo](https://discourse.julialang.org/u/melonedo)\
**Post date:** [January 3, 2022, 5:22pm UTC](https://discourse.julialang.org/t/how-to-pass-arguments-to-cmd/73959/6 "2022-01-03T17:22:47Z")

</div>

Thanks! Such a shame that only after your reply did I realize the manual had already mentioned this situation.

I do know that commands are an array of strings, but it is surprising to find out that Julia did it the right way!
