Skip to content

Command lines and how they handle single and double quotes

Juergen Donnerstag edited this page Oct 8, 2021 · 3 revisions

Problem

When you are reading this, then you probably know that I've been developing a native V-lang implementation of ROSIE. Like the original implementation, I have a cli and several subcommands support that users provide a rpl (pattern; think regex expression). Within the RPL string literals can be escaped with either single or double quotes. Spaces are also very common. E.g. "a" ~ "b" is a pattern that matches "a", then a word boundary, and then "b".

First I thought it is a V-lang issue in how it processes command line arguments.

I looked at V's code and didn't see anything suspicous. I even did some test_..() with the functions used, and nothing weird happened. I really had no clue what was happening and reverted to the idea that it must be how CMD, PS, bash etc treat the command line and found a number of enlightning blogs.

http://www.windowsinspired.com/understanding-the-command-line-string-and-arguments-received-by-a-windows-program/ This first one is great in explaining how CMD works, and double quotes: "Double quote characters in a command line string have no relation to the boundaries between arguments and do not necessarily enclose arguments. Each individual double quote character by itself acts simply as a switch to enable or disable the recognition of space as a divider between arguments.". Which is why echo '"a"' is the same as echo 'a' and the same as echo "'a'" in CMD.

PS handles single and double quote slightly different, and especially escaping is different.

In summary, depending on CMD, PS or bash, the command line and escapes are different.

I'm using VS-Code and usually CMD as my terminal, and what works for me (Win10): "a" ~ "b" => rosie grep "\"a\" ~ \"b\""

V compiler tweak to it

Consider you are developing an application and want to run it with the suggestions from above, e.g. CMD: v run . "\"mydata\"". Then you'll realize that it doesn't work (not with the latest version at the time of writing). The reason being that two shell invocations happen behind the scene. One for V itself and one for your application. With that knowledge in mind, a work-around looks like CMD: v run . "\\\"mydata\\\""

Clone this wiki locally