# Proper escaping when building a command with nested quotes

**URL:** <https://discourse.julialang.org/t/proper-escaping-when-building-a-command-with-nested-quotes/31432>\
**Category:** New to Julia\
**Tags:** question, shell\
**Created:** [November 23, 2019, 8:23pm UTC](https://discourse.julialang.org/t/proper-escaping-when-building-a-command-with-nested-quotes/31432 "2019-11-23T20:23:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dapz](https://avatars.discourse-cdn.com/v4/letter/d/a4c791/32.png) [@dapz](https://discourse.julialang.org/u/dapz)\
**Post date:** [November 23, 2019, 8:23pm UTC](https://discourse.julialang.org/t/proper-escaping-when-building-a-command-with-nested-quotes/31432/1 "2019-11-23T20:23:35Z")

</div>

Hi, I was wondering if there is a “rule” to write commands with nested quotes that work. Here is an example with 3 nested commands (bash calling bash calling julia). My final goal is slightly different (a julia call passed to a program which is itself called through ssh), but I hope if I can get this example to work I will understand better how to solve the problem.

So here is what I tried.

```julia
e = quote println("It works") end
jcall = "julia -e '$(string(e))'"
cmd = `bash -c $jcall`
# run(cmd) # OK!

cmd2 = `bash -c $cmd`
# run(cmd2) # Does not work

```

So here I don’t understand, I thought building a command with backticks would ensure proper escaping (or even avoid needing to escape, if the arguments are passed directly to the command without running a shell, not sure I really understood everything here).  
Then I tried:

```julia
cmd_str = "bash -c \"$jcall\""
cmd2 = `bash -c $cmd_str`
# run(cmd2) # does not work

jcall = "julia -e \\\'$(string(e))\\\'"
cmd_str = "bash -c \'$jcall\'"
cmd2 = `bash -c $cmd_str`
# run(cmd2) # does not work

cmd_custom = `bash -c "bash -c \"julia -e 'begin println(\\\"It works\\\") end'\""`
println(cmd_custom)
# run(cmd_custom) # ok

jcall = "julia -e '$(string(e))'"
cmd_str = "bash -c \"$(escape_string(jcall))\""
cmd2 = `bash -c $(cmd_str)`
println(cmd2)
# run(cmd2) # does not work ("\" is not a unary operator)

```

I especially don’t understand the last example, which I thought would work (Is there a different escape level between the quotes and the \n ? Or what is wrong ?)
