How to pass arguments to Cmd?

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

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 from spaces or metacharacters in the string.)

8 Likes