# Convert string containing quotes to command

**URL:** https://discourse.julialang.org/t/convert-string-containing-quotes-to-command/101334
**Category:** General Usage
**Tags:** question, strings, cmd
**Created:** [July 7, 2023, 10:50pm UTC](https://discourse.julialang.org/t/convert-string-containing-quotes-to-command/101334 "2023-07-07T22:50:34Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![maths.tex](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@maths.tex](https://discourse.julialang.org/u/maths.tex)
#### Post date: [July 7, 2023, 10:50pm UTC](https://discourse.julialang.org/t/convert-string-containing-quotes-to-command/101334/1 "2023-07-07T22:50:35Z")

</div>

I’m new to julia. I have the following strings readen from a json file:

pathtoexe = “c:\my soft\myexe.exe”  
folder = “c:\folder with spaces”  
mycommand= “‘pathtoexe’ /option /D=‘folder’”

The following commands replace pathtoexe and folder with their values in mycommand:

mycommand=replace(mycommand, “pathtoexe” =\> pathtoexe)  
mycommand=replace(mycommand, “folder” =\> folder)

This gives “‘c:\my soft\myexe.exe’ /option /D=‘c:\folder with spaces’”, which is great.  
Now i need to convert mycommand to a command before running it. Both commands:

cmd =`$mycommand`  
and  
cmd = @cmd(mycommand)

add extras “” that breaks it:

They gives `"'c:\my soft\myexe.exe' /option /D='c:\folder with spaces'"`  
instead of `'c:\my soft\myexe.exe' /option /D='c:\folder with spaces'`

then run fails.

How could i properly convert the string to a command ?

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [July 7, 2023, 10:55pm UTC](https://discourse.julialang.org/t/convert-string-containing-quotes-to-command/101334/2 "2023-07-07T22:55:36Z")

</div>

Try interpolating your `pathtoexe` etc directly into the command, with quoting as necessary.

[https://docs.julialang.org/en/v1/manual/running-external-programs/#command-interpolation](https://docs.julialang.org/en/v1/manual/running-external-programs/#command-interpolation)

Something like this:

```julia
  cmd = `'$pathtoexe' /option /D='$folder'`

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [July 8, 2023, 12:29am UTC](https://discourse.julialang.org/t/convert-string-containing-quotes-to-command/101334/3 "2023-07-08T00:29:12Z")

</div>

> [@Jeff\_Emanuel](#):
>
> with quoting as necessary.

You shouldn’t need to quote. Command strings aren’t a shell.

---

<div class="post-metadata">

### Author: ![maths.tex](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@maths.tex](https://discourse.julialang.org/u/maths.tex)
#### Post date: [July 8, 2023, 11:54am UTC](https://discourse.julialang.org/t/convert-string-containing-quotes-to-command/101334/4 "2023-07-08T11:54:44Z")

</div>

Exact, the problem is that i can’t write in the json file:

```julia
 "mycommand": `'$pathtoexe' /option /D='$folder'`,

```

because after

```julia
mycommand=get!(mydict, "mycommand",0)

```

fails. And if i write in json

```julia
"mycommand": "`'$pathtoexe' /option /D='$folder'`",

```

then

```julia
mycommand=get!(mydict, "mycommand",0) 

```

returns “`'\$pathtoexe' /option /D='\$folder'`”, thus escaping $ and not interpolating.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [July 8, 2023, 12:08pm UTC](https://discourse.julialang.org/t/convert-string-containing-quotes-to-command/101334/5 "2023-07-08T12:08:19Z")

</div>

> [@maths.tex](#):
>
> Exact, the problem is that i can’t write in the json file:

If you’re going to store a command in a JSON file, you should store it as list of strings, one per argument, not as a single string. This is how Julia stores a `Cmd`, actually.

You can call `collect(cmd)` (or `cmd.exec`) to get a list of strings from a `cmd::Cmd`, and you can call `Cmd(list_of_strings)` to reconstruct the `Cmd`.

(To store it as a single string, you’d have to do some parsing to break it up into arguments, and it’s easy to muck that up if there are spaces or other special characters.)

> [@maths.tex](#):
>
> returns “`'\$pathtoexe' /option /D='\$folder'`”, thus escaping $ and not interpolating.

If you want to load a string/command from a file that has a `$expression` and _then_ interpolate the value of the expression, then you basically have to invoke the Julia interpreter via `eval`, or write a custom interpreter.

---

<div class="post-metadata">

### Author: ![maths.tex](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@maths.tex](https://discourse.julialang.org/u/maths.tex)
#### Post date: [July 28, 2023, 9:46pm UTC](https://discourse.julialang.org/t/convert-string-containing-quotes-to-command/101334/6 "2023-07-28T21:46:49Z")

</div>

Thank you @stevengj for your answer, and sorry for this late reply.

I made the json line as you suggested:  
`mycommand= ["$pathtoexe","/option","/D=$folder"]`

then the following instruction cancelled `&` changing it to `\$`, thus didn’t interpolate:  
`mycommand=Vector{String}(get!(soft, "mycommand",0))`

But i could manage to get the desired result applying `replace.` to the list of string, before making the command:

```julia
mycommand = ["pathtoexe","/option","/D=folder"] (in json)
mycommand .=replace.(mycommand, "pathtoexe" => pathtoexe)
mycommand .=replace.(mycommand, "folder" => folder)
mycommand =Cmd(mycommand)

```

This is a satisfying solution for my needs, therefore I didn’t try your other idea with `eval`.  
Thanks for your help.
