1

I currently have a python script that takes multiple arguments, one of which happens to be a triple quoted string that are passed to script like so:

Script.py --FunctionArgs '"osPlatform='SUSE',osVersion=11"'

As you can see there are outer single quotations, with inner double quotes with a further pair of single quotations. It's vital that these inner single quotations stay when used in the script however when I print sys.args I get the following:

(FunctionArgs='"osPlatform=SUSE,osVersion=11"')

As you can see the inner quotes are stripped out. Due to the nature of the how the script is run I cannot do anything about the strange triple-quote formatting of the argument. Is there anyway I can get my Python script to not strip these inner quotes from the argument?

When the FunctionArgs parameter doesn't have the outer single quotes (something I can't do anything about) it works fine and the inner single quotes are retained:

Script.py --FunctionArgs "osPlatform='SUSE',osVersion=11"

Results in

(FunctionArgs="osPlatform='SUSE',osVersion=11")
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    the single quote before `SUSE` is matching the single quote that starts the argument, it's not part of the argument. That's how shell command line parsing works. – Barmar Apr 25 '19 at 21:27
  • 1
    It's not happening in the Python script, the shell does this before invoking the script. – Barmar Apr 25 '19 at 21:28
  • 1
    You have to fix the script that's invoking python, there's absolutely nothing you can do in the python script to restore it. – Barmar Apr 25 '19 at 21:29
  • Ahh I see, would the only way to ensure it doesn't get treated as the closing quote be by escaping it? –  Apr 25 '19 at 21:30
  • There's no escaping inside single quotes. – Barmar Apr 25 '19 at 21:33
  • @Barmar I see, seems like as I feared then, going to have to change the way the argument is formed. Thanks for the clarification though! –  Apr 25 '19 at 21:35
  • It depends on the shell you are using... for SH/BASH Barmer gave you the answer. – user518450 Apr 25 '19 at 21:41
  • 1
    Possible duplicate of [Triple nested quotations in shell script](https://stackoverflow.com/questions/21168817/triple-nested-quotations-in-shell-script) – Prune Apr 25 '19 at 22:09

1 Answers1

2

The quotes around SUSE are matching the quotes that delimit the argument. The shell removes them during its command line parsing.

You can put the entire argument in double quotes, and escape the embedded double quotes.

Script.py --FunctionArgs "\"osPlatform='SUSE',osVersion=11\""
Barmar
  • 741,623
  • 53
  • 500
  • 612