# Help with a shell command

**URL:** https://discourse.julialang.org/t/help-with-a-shell-command/45312
**Category:** New to Julia
**Created:** [August 21, 2020, 8:25am UTC](https://discourse.julialang.org/t/help-with-a-shell-command/45312 "2020-08-21T08:25:24Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Joachim\_Dahl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joachim_dahl/32/17369_2.png) [@Joachim\_Dahl](https://discourse.julialang.org/u/Joachim_Dahl)
#### Post date: [August 21, 2020, 8:25am UTC](https://discourse.julialang.org/t/help-with-a-shell-command/45312/1 "2020-08-21T08:25:24Z")

</div>

I am trying to construct a shell command for running  
$ python3 script.py X=“a b c”

Here’s what I tried:

```julia
julia> args = ["script.py", "X=\"a b c\""]
2-element Array{String,1}:
 "script.py"  
 "X=\"a b c\""

julia> cmd = `python3 $args`
`python3 script.py 'X="a b c"'`

```

How can I get rid of the extra ticks?

---

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [August 21, 2020, 12:08pm UTC](https://discourse.julialang.org/t/help-with-a-shell-command/45312/2 "2020-08-21T12:08:28Z")

</div>

```julia
function escstr(str)
        arr = collect(str); # Split string into an array of characters
        resultstr = Array{eltype(arr)}(undef,0)
        # Foreach character do any substitutions neccesary        
        for c in arr
            if c == '"'
                push!(resultstr,'\\','"')
            elseif c == '\\'
                push!(resultstr,'\\','\\')
            else
                push!(resultstr,c)
            end
        end
        result = join(resultstr)
        return result
    end

    bashit(str) = run(`bash -c "$str"`)

   pstr = escstr( raw""" script.py X="a b c" """)
   
   bashit("python3 $(pstr)")

```

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [August 21, 2020, 1:13pm UTC](https://discourse.julialang.org/t/help-with-a-shell-command/45312/3 "2020-08-21T13:13:13Z")

</div>

There’s no extra ticks, that’s just for display. Also definitely don’t use shell to run command.

---

<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: [August 21, 2020, 1:59pm UTC](https://discourse.julialang.org/t/help-with-a-shell-command/45312/4 "2020-08-21T13:59:07Z")

</div>

If the argument is typed as `X="a b c"` in the shell, then the actual argument passed to the Python script is `X=a b c` because the shell interprets the double quotes and removes them but keeps the resulting quoted string as a single argument instead of splitting it on spaces as it would without the quotes. You can do the exact same thing when constructing a Julia command literal:

```julia
julia> cmd = `python3 script.jl X="a b c"`
`python3 script.jl 'X=a b c'`

julia> collect(cmd)
3-element Vector{String}:
 "python3"
 "script.jl"
 "X=a b c"

```

Julia command literals interpret double quotes just like the shell does. By splicing a string value of `X="a b c"` into a command, on the other hand, you are indicating that you actually want the double quotes to be part of the argument rather than interpreted by the command literal (the command literal only interprets what is literally there). If instead you interpolate the exact string that you want the Python script to see, i.e. `X=a b c`, then it should do what you want:

```julia
julia> arg = "X=a b c"
"X=a b c"

julia> cmd = `python3 script.jl $arg`
`python3 script.jl 'X=a b c'`

```

Or similarly, you can do this:

```julia
julia> vars = "a b c"
"a b c"

julia> cmd = `python3 script.jl X=$vars`
`python3 script.jl 'X=a b c'`

```

Note that the exact string value that you pass as `arg` is exactly what the python script sees as the value of that argument. It completely bypasses any shell quoting or unquoting shenanigans. The fact that you think you need to put double quotes in there is causing all the confusion and that’s just so that you can prevent the shell from splitting the value on spaces, which you don’t want, which then causes further confusion because the shell also removes the double quotes which you didn’t actually want in the argument at all, you only had to add them to prevent space splitting.
