[issue19430] argparse replaces \$ with $ (if in commandline)

R. David Murray report at bugs.python.org
Tue Oct 29 13:14:47 CET 2013


R. David Murray added the comment:

Indeed, you can see in the original posting that the \ is already gone from the dollar in sys.argv, so argparse has nothing to do with it.

And it is indeed the shell doing the unescaping:

rdmurray at session:~>cat test.sh
#!/bin/bash
echo "$@"
rdmurray at session:~>bash test.sh "abc [\t] \$"
abc [\t] $
rdmurray at session:~>bash test.sh 'abc [\t] \$'
abc [\t] \$
rdmurray at session:~>bash test.sh 'abc [\t] \$'
abc [\t] \$
rdmurray at session:~>bash test.sh "'abc [\t] \$'"
'abc [\t] $'
rdmurray at session:~>bash test.sh "'abc [\t] \\$'"
'abc [\t] \$'
rdmurray at session:~>bash test.sh '"abc [\t] \\\$foo"'
"abc [\t] \\\$foo"
rdmurray at session:~>bash test.sh '"abc [\t] \$foo"'  
"abc [\t] \$foo"

The shell treats $ specially because $ has a special meaning inside double quotes (variable substitution), so it presumably unescapes it as part of the double quote string processing.  You have to escape both the backslash and the $ if you want a literal '\$' to wind up in argv, when the outer level of quoting is double quotes.

----------
nosy: +r.david.murray
resolution:  -> invalid
stage:  -> committed/rejected
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19430>
_______________________________________


More information about the Python-bugs-list mailing list