How to make Windows command prompt treat single quote as though it is a double quote?

My scenario is simple - I am copying script samples from the Mercurial online book and pasting them in a Windows command prompt. The problem is that the samples in the book use single quoted strings. When a single quoted string is passed on the Windows command prompt, the latter does not recognize that everything between the single quotes belongs to one string.

For example, the following command:

hg commit -m 'Initial commit'

cannot be pasted as is in a command prompt, because the latter treats 'Initial commit' as two strings - 'Initial and commit'. I have to edit the command after paste and it is annoying.

Is it possible to instruct the Windows command prompt to treat single quotes similarly to the double one?

EDIT

Following the reply by JdeBP I have done a little research. Here is the summary:

  • Mercurial entry point looks like so (it is a python program):

    def run():
        "run the command in sys.argv"
        sys.exit(dispatch(request(sys.argv[1:])))
    
  • So, I have created a tiny python program to mimic the command line processing used by mercurial:

    import sys
    print sys.argv[1:]
    
  • Here is the Unix console log:

    [hg@Quake ~]$ python 1.py  "1 2 3"
    ['1 2 3']
    [hg@Quake ~]$ python 1.py  '1 2 3'
    ['1 2 3']
    [hg@Quake ~]$ python 1.py  1 2 3
    ['1', '2', '3']
    [hg@Quake ~]$
    
  • And here is the respective Windows console log:

    C:\Work>python 1.py  "1 2 3"
    ['1 2 3']
    
    C:\Work>python 1.py  '1 2 3'
    ["'1", '2', "3'"]
    
    C:\Work>python 1.py  1 2 3
    ['1', '2', '3']
    
    C:\Work>
    

One can clearly see that Windows does not treat single quotes as double quotes. And this is the essence of my question.


The quoting character can't be changed in the command.com prompt. You can, however, use PowerShell which accepts both single and double quotes as quoting characters. They function the same as in Unix shells. I.e., single quotes do not expand variables while double quotes will.

You might still run into problems with quotes inside quotes. For example, I have strawberry perl installed on my Windows computer. When I run perl -e 'print time, "\n" ' in PowerShell, I see output such as 1321375663SCALAR(0x15731d4). I have to escape the double quotes for it to work as expected: perl -e 'print time, \"\n\" '


First, a command prompt is not a command interpreter. (A command prompt is the thing displayed by a command interpreter.) Second, your command interpreter, the prompts that it issues, and Win32 consoles, have nothing at all to do with this.

In Win32 programs, splitting up the command line into "words" — the NUL-terminated multi-byte character strings that programs in the C and C++ languages see as the argument array passed to main() — is the province of the runtime libraries of those programs. On Unices and Linux, the shell does the word splitting, because the operating system actually works in terms of an argument string array. This is not the case for Win32. On Win32, the operating system itself operates in terms of a command tail: a single long string that still contains all of the quotation marks that one originally typed on the command line. (There is some processing done to this command tail by a command interpreter before it is passed to the target program, but it isn't related to word splitting.)

In your case, the runtime library for your hg program is being delivered this command tail:

commit -m 'Initial commit'

The runtime library that that program was compiled with doesn't know that you meant a single quotation mark to be a whitespace quoting character, because that isn't the convention. The convention deals only in double quotation marks (and backslashes before double quotation marks).

This convention is built into the runtime library that was provided with the compiler used to create the program in the first place. If you want to change the convention, you'll have to re-link every individual program that you want to run this way with a special runtime library of your own making that recognizes single quotation marks as well. Clearly this is impractical (unless these are all Cygwin programs).

A far more practical approach is to do what you're already doing: recognize that Windows isn't Unix, and adjust the examples accordingly before using them.