# External program with string argument interpolation

**URL:** <https://discourse.julialang.org/t/external-program-with-string-argument-interpolation/47134>\
**Category:** General Usage\
**Tags:** cmd\
**Created:** [September 23, 2020, 12:51pm UTC](https://discourse.julialang.org/t/external-program-with-string-argument-interpolation/47134 "2020-09-23T12:51:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [September 23, 2020, 12:51pm UTC](https://discourse.julialang.org/t/external-program-with-string-argument-interpolation/47134/1 "2020-09-23T12:51:32Z")

</div>

Hi,  
I can’t figure how (after reading [https://docs.julialang.org/en/v1/manual/running-external-programs/manual](https://docs.julialang.org/en/v1/manual/running-external-programs/manual)) to run a command with string arguments enclosed with “”

Le’s say the shell command I want to invoke is

> shell\> echo “toto”

then

> julia\> run(`echo "toto"`)

actually corresponds to

> shell\> echo toto

without ""s  
How can I preserve the ""s ?

P.S. I know that my example is not relevant. I am actually dealing with the following command:

> gdb -batch -ex “disassemble/rs dexp” /tmp/libdexp\_C.so

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [September 23, 2020, 1:10pm UTC](https://discourse.julialang.org/t/external-program-with-string-argument-interpolation/47134/2 "2020-09-23T13:10:09Z")

</div>

This?

```julia
julia> run(`echo \"toto\"`)
"toto"
Process(`echo '"toto"'`, ProcessExited(0))

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 23, 2020, 1:12pm UTC](https://discourse.julialang.org/t/external-program-with-string-argument-interpolation/47134/3 "2020-09-23T13:12:39Z")

</div>

What happens if you do:

```julia
julia> cmd=`gdb -batch -ex "disassemble/rs dexp" /tmp/libdexp_C.so`

julia> run(cmd)

```

Because, as you can see:

```julia
julia> dump(cmd)
Cmd
  exec: Array{String}((5,))
    1: String "gdb"
    2: String "-batch"
    3: String "-ex"
    4: String "disassemble/rs dexp"
    5: String "/tmp/libdexp_C.so"
  ignorestatus: Bool false
  flags: UInt32 0x00000000
  env: Nothing nothing
  dir: String ""

```

ARGV[4] is the complete string not splitted by the space. So I expect it to work as you want to have it.

Noteworthy: run doesn’t run the command in a shell :[Running External Programs · The Julia Language](https://docs.julialang.org/en/v1/manual/running-external-programs/#Running-External-Programs)

> The command is never run with a shell. Instead, Julia parses the command syntax directly, appropriately interpolating variables and splitting on words as the shell would, respecting shell quoting syntax. The command is run as `julia` 's immediate child process, using `fork` and `exec` calls.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 23, 2020, 1:17pm UTC](https://discourse.julialang.org/t/external-program-with-string-argument-interpolation/47134/4 "2020-09-23T13:17:09Z")

</div>

Interpolation should be straight forward: [Running External Programs · The Julia Language](https://docs.julialang.org/en/v1/manual/running-external-programs/#command-interpolation)

---

<div class="post-metadata">

**Author:** ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)\
**Post date:** [September 23, 2020, 1:27pm UTC](https://discourse.julialang.org/t/external-program-with-string-argument-interpolation/47134/5 "2020-09-23T13:27:54Z")

</div>

Thank you very much !

It did not work because my symbol was not defined and I (wrongly) guessed that it was an interpolation issue…

Sorry for the noise and thank you for the `dump(cmd)` example : it helps a lot !

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [September 23, 2020, 9:40pm UTC](https://discourse.julialang.org/t/external-program-with-string-argument-interpolation/47134/6 "2020-09-23T21:40:41Z")

</div>

> [@oheil](#):
>
> Interpolation should be straight forward

Yes, instead of fiddling with quotes, I tend to rely on variable interpolation in such cases:

```julia-auto
julia> complex_str = """foo "bar" \t baz"""
"foo \"bar\" \t baz"

julia> cmd = `echo $complex_str`
`echo 'foo "bar" baz'`

julia> collect(cmd)
2-element Array{String,1}:
 "echo"
 "foo \"bar\" \t baz"

julia> run(cmd)
foo "bar" baz
Process(`echo 'foo "bar" baz'`, ProcessExited(0))

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [September 23, 2020, 9:55pm UTC](https://discourse.julialang.org/t/external-program-with-string-argument-interpolation/47134/7 "2020-09-23T21:55:46Z")

</div>

Just because it hasn’t been brought up yet: Enquoting the string argument in single quotes works similarly to other Posix shells, in that it escapes everything except `'` itself and ```, so in your example `run(`echo '"toto"'`)` should also just work.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 24, 2020, 7:31am UTC](https://discourse.julialang.org/t/external-program-with-string-argument-interpolation/47134/8 "2020-09-24T07:31:58Z")

</div>

The difference is, that with this the double quotes `"` are passed as part of the ARG value, which may be not wanted.  
In the shell you want:  
“a string with space” passed as single argument value, so you do e.g.:

```julia
bash > some_cmd -input "a string with space"

```

Now argv[2]=a string with space  
(without the `"`)  
In the case of

```julia
run(`some_cmd -input '"a string with space"'`)

```

which is identical to

```julia
run(`some_cmd -input \"a string with space\"`)

```

some\_cmd now receives as argv[2]:  
`"a string with space"`  
Where double quotes are part of the string.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [September 24, 2020, 7:40am UTC](https://discourse.julialang.org/t/external-program-with-string-argument-interpolation/47134/9 "2020-09-24T07:40:48Z")

</div>

To clarify by example, in the shell this is what the question is about:

```julia
bash> echo "1 2 3" | egrep -e 1 2 3
grep: 2: No such file or directory
grep: 3: No such file or directory

```

ok, easy, just do:

```julia
bash> echo "1 2 3" | egrep -e "1 2 3"
1 2 3

```

Yeah, works.  
Now Julia’s backticks would do:

```julia
bash> echo "1 2 3" | egrep -e "\"1 2 3\""

```

or

```julia
bash> echo "1 2 3" | egrep -e '"1 2 3"'

```

Result empty, of course, the douple quotes are not echoed, so no match by grep.
