Quoting special characters of a url in Cmd objects on Windows

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

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

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

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

When the url contains special characters, you need to add quotes and an extra argument on the command line:

> 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> 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?

I found this solution:

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)

1 Like

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. :slight_smile:

1 Like

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

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> 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.

1 Like

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

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> 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 :upside_down_face:

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> c.flags
0x00000004

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

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.

1 Like