Why is there extra single quotes in this command?
args = ["--arg1 hi.txt", "--arg2 bye.txt"]
cmd = `myscript $args`
`myscript '--arg1 hi.txt' '--arg2 bye.txt'`
How do I get just
`myscript --arg1 hi.txt --arg2 bye.txt`
Why is there extra single quotes in this command?
args = ["--arg1 hi.txt", "--arg2 bye.txt"]
cmd = `myscript $args`
`myscript '--arg1 hi.txt' '--arg2 bye.txt'`
How do I get just
`myscript --arg1 hi.txt --arg2 bye.txt`
The command interpolation is doing exactly what you’ve asked for (but not what you wanted). It’s running the command myscript
with two arguments, and each of those arguments happens to have a space in it. Presumably myscript
actually expects to be invoked with 4 arguments (the way your shell would interpret 4 space-separated arguments), which you can get like this:
julia> args = ["--arg1", "hi.txt", "--arg2", "bye.txt"];
julia> cmd = `myscript $args`
`myscript --arg1 hi.txt --arg2 bye.txt`
Note that quote marks are kind of a red herring here. Doing myscript '--arg1'
and myscript --arg1
in your shell makes no difference whatsoever. The key is whether the space between --arg1
and hi.txt
actually creates separate arguments or not.
You can see this more clearly by creating a file test.jl
containing this:
@show ARGS # print the list of command line arguments
Now let’s try it from a shell:
$ julia test.jl hello world
ARGS = ["hello", "world"]
$ julia test.jl 'hello' world
ARGS = ["hello", "world"]
$ julia test.jl 'hello world'
ARGS = ["hello world"]
Single quotes around an argument have no effect. But quoting the string hello world
turns it from a pair of separate arguments into a single argument with a space in the middle.
The command interpolation is giving you the same choice. Let’s run test.jl
from a Julia cmd:
julia> args = ["hello world"] # one argument, with a space in it
1-element Vector{String}:
"hello world"
julia> cmd = `julia test.jl $args`
`julia test.jl 'hello world'`
# Prints a single argument with a space in it, as expected
julia> run(cmd)
ARGS = ["hello world"]
Process(`julia test.jl 'hello world'`, ProcessExited(0))
# Two actually separate arguments
julia> args = ["hello", "world"]
2-element Vector{String}:
"hello"
"world"
julia> cmd = `julia test.jl $args`
`julia test.jl hello world`
# Prints the two separate arguments, as expected
julia> run(cmd)
ARGS = ["hello", "world"]
Process(`julia test.jl hello world`, ProcessExited(0))