1

If I have a Python program like this (substitute with any similar program):

import sys
s = sys.argv[1]
print(s)

It just prints the argument provided. With Windows cmd.exe, if I want to provide a literal redirect character >, I need to quote it:

C:\> python a.py ">"
>

and if I want to provide a literal double quote character, I need to use this syntax:

C:\> python a.py """
"

My understanding is that with the second example, a literal double quote is only recognized if it immediately follows a closing quote. So that example reads as Open Quote, Close Quote, Literal Quote. However I noticed something strange, this example:

C:\> python a.py """>
">

From my understanding, it seems the shell passed a literal redirect character in error. If I understand the parsing, it should be Open Quote, Close Quote, Literal Quote, Redirect (not literal). Is this in fact an error, or have I misunderstood the parsing rules? Note I tested with Windows 10 and Windows 7.

2
  • 1
    each application in Windows parses the command parameters themselves, so the result may differ from an app to another. Most applications use the default Windows' libc implementation but cmd uses a different one, and python may in turn uses its own parsing rule. Use PowerShell instead which has a standard rule that makes passing special characters easier
    – phuclv
    May 29, 2020 at 3:04
  • What happens if you try: python a.py \">\" ?? But, this seems work: python a.py """>""" see this answer
    – Io-oI
    May 29, 2020 at 5:10

1 Answer 1

0

The issue is, that the parsing rules as I understood them changed in 2008:

http://daviddeley.com/autohotkey/parameters/parameters.htm#WINCRULES

Starting with C Runtime msvcr90.dll, command line argument parsing has been improved. So instead of this:

C:\> python-2.5.4.amd64\python.exe a.py "a ""b"""" c"
a "b" c

Where the result is:

" open
a

" close
" literal
b
" open
" close
" literal
" open

c
" close

You can do this:

C:\> python-2.6.2.amd64\python.exe a.py "a ""b"" c"
a "b" c

Where the result is:

" open
a

" escape
" literal
b
" escape
" literal

c
" close

Finally, to address the orignal example:

" open
" escape
" literal
> literal

The correct command (in the context of sending one double quote and redirecting the result) would be:

"""">

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .