# Quoting special characters of a url in Cmd objects on Windows

**URL:** <https://discourse.julialang.org/t/quoting-special-characters-of-a-url-in-cmd-objects-on-windows/44324>\
**Category:** General Usage\
**Created:** [August 5, 2020, 11:47am UTC](https://discourse.julialang.org/t/quoting-special-characters-of-a-url-in-cmd-objects-on-windows/44324 "2020-08-05T11:47:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![NiclasMattsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niclasmattsson/32/21988_2.png) [@NiclasMattsson](https://discourse.julialang.org/u/NiclasMattsson)\
**Post date:** [August 5, 2020, 11:47am UTC](https://discourse.julialang.org/t/quoting-special-characters-of-a-url-in-cmd-objects-on-windows/44324/1 "2020-08-05T11:47:13Z")

</div>

To open a webpage using the default browser in the Windows command line you can use:

```nohighlight
> start https://www.google.com/

```

This works from Julia too if you prefix it with `cmd /c`:

```julia
julia> run(`cmd /c start https://www.google.com/`)

```

When the url contains special characters, [you need to add quotes and an extra argument](https://superuser.com/a/36730) on the command line:

```nohighlight
> start "" "http://maps.google.com/maps?t=k&q=loc:51.508062+-0.076051"

```

But the Julia version of that line fails, as does every other combination I’ve tried using single quotes, double quotes and backslashes. It just opens the truncated url `https://www.google.com/maps?t=k`.

```julia
julia> run(`cmd /c start "" "http://maps.google.com/maps?t=k&q=loc:51.508062+-0.076051"`)
'q' is not recognized as an internal or external command,
operable program or batch file.
ERROR: failed process: Process(`cmd /c start '' 'http://maps.google.com/maps?t=k&q=loc:51.508062+-0.076051'`, ProcessExited(1)) [1]

```

So how can I run that call to Google maps from Julia?

---

<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:** [August 5, 2020, 12:22pm UTC](https://discourse.julialang.org/t/quoting-special-characters-of-a-url-in-cmd-objects-on-windows/44324/2 "2020-08-05T12:22:34Z")

</div>

I found this solution:

```julia
c=Cmd(`cmd /c start \"\" "http://maps.google.com/maps?t=k\"&\"q=loc:51.508062+-0.076051"`,windows_verbatim=true)
run(c)

```

which is not obvious ~~and may only work for Windows.~~ (of course only windows because of cmd)

---

<div class="post-metadata">

**Author:** ![NiclasMattsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niclasmattsson/32/21988_2.png) [@NiclasMattsson](https://discourse.julialang.org/u/NiclasMattsson)\
**Post date:** [August 5, 2020, 12:34pm UTC](https://discourse.julialang.org/t/quoting-special-characters-of-a-url-in-cmd-objects-on-windows/44324/3 "2020-08-05T12:34:57Z")

</div>

Wow, thanks! I hesitated for the longest time before I clicked the like button because that is so darn ugly, but I finally realized that’s not on you. 🙂

---

<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:** [August 5, 2020, 12:45pm UTC](https://discourse.julialang.org/t/quoting-special-characters-of-a-url-in-cmd-objects-on-windows/44324/4 "2020-08-05T12:45:57Z")

</div>

Proper quoting can become ugly really fast, it is not Julia specific.

---

<div class="post-metadata">

**Author:** ![NiclasMattsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niclasmattsson/32/21988_2.png) [@NiclasMattsson](https://discourse.julialang.org/u/NiclasMattsson)\
**Post date:** [August 5, 2020, 1:02pm UTC](https://discourse.julialang.org/t/quoting-special-characters-of-a-url-in-cmd-objects-on-windows/44324/5 "2020-08-05T13:02:26Z")

</div>

I’m used to all the backslashes since I have plenty of experience with regexes, but this was really unusual. This is what you get if you print the `c` variable that you create:

```julia
julia> c
`cmd /c start '""' 'http://maps.google.com/maps?t=k"&"q=loc:51.508062+-0.076051'`

```

It seems that the ampersand was escaped by surrounding it with double-quotes in the middle of a single-quoted string. That’s what I found so bizarre. Is that a Windows requirement? Anyway, I wouldn’t have figured out that syntax in a million years so all the more power to you.

---

<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:** [August 5, 2020, 1:26pm UTC](https://discourse.julialang.org/t/quoting-special-characters-of-a-url-in-cmd-objects-on-windows/44324/6 "2020-08-05T13:26:18Z")

</div>

First I tried to reproduce the error on a plain CMD without Julia and thats what produces the error:

```julia
C:\Users\oheil>start "" http://maps.google.com/maps?t=k&q=loc:51.508062+-0.076051

```

just without " around the URL.

Than I looked in Julia for c.exec, here for the direct attempt without special quoting:

```julia
julia> c=Cmd(`cmd /c start "" "http://maps.google.com/maps?t=k&q=loc:51.508062+-0.076051"`)
`cmd /c start '' 'http://maps.google.com/maps?t=k&q=loc:51.508062+-0.076051'`

julia> c.exec
5-element Array{String,1}:
 "cmd"
 "/c"
 "start"
 ""
 "http://maps.google.com/maps?t=k&q=loc:51.508062+-0.076051"

```

So my guess was, that the surrounding " aren’t passed with the arguments so some special quoting needs to be done.

The next steps were just trail and error with some implausible imagination 🙃

The real issue here is, that the next time someone looks for proper quoting, this solution here may not help at all, because c.exec is identical with or without `windows_verbatim=true`. The difference is in c.flags:

```julia
julia> c.flags
0x00000004

```

and setting `windows_verbatim=true` seems to be contra-intuitive:

```julia
help?> Cmd
...
    • windows_verbatim::Bool: If true (defaults to false), then on Windows the Cmd will send a command-line string to the
         process with no quoting or escaping of arguments, even arguments containing spaces...

```

So looking for proper quoting the default `false` makes more sense, but I didn’t found a way with that.
