This was going to be a question, but then it turned into a "today I
learned...", and finally is a question again, but a harder one. I'm
including the TIL in the beginning, with the new question at the end.
My problem: I was trying to figure out how to include a literal
single-quote (a.k.a. apostrophe) in a parameter that I was passing to
a Python script that I was calling via QSH.
I know about the escape-by-doubling rule, so I thought it would be:
qsh cmd('python3 script.py ''with space'' '''with''''quote''')
To make it easier to read, if you're not copying and pasting, let's
pretend ^ is the single-quote character. Then my attempt was:
qsh cmd(^python3 script.py ^^with space^^ ^^with^^^^quote^^^)
The literal space in the first parameter came through fine. But the
second parameter actually became
withquote
That is, the (re)doubled quotes completely canceled themselves out and
went away. I tried quite many things, including some that involved
backslashes, and at my wits' end I found this, which asks about spaces
rather than quotes:
https://www.ibm.com/developerworks/community/forums/html/topic?id=7e8784cc-ad3e-4a1e-b125-1e534191d344
Yeah, it's on dW, with a big banner at the top that says it's
available for read-only until the end of March, at which point it will
be completely gone. I'd rather not regurgitate the entire contents
here because my post is too long as it is (sorry, future archive
readers!).
The answer was provided by Barbara Morris. In short, she says to
simply use double-quotes as parameter delimiters! So in my case, it
would be
qsh cmd('python3 script.py "with space" "with''quote"')
Note that the literal single-quotes are now doubled instead of
quadrupled. Again, for easier reading, let's pretend ^ is
single-quote, and we'll use # for double-quote:
qsh cmd(^python3 script.py #with space# #with^^quote#^)
All is great, except Barbara points out, for completeness, that she
doesn't know what to do if you want a literal double-quote. Well, my
earlier attempt shows that you can use single-quotes as your parameter
delimiters, so you just do this:
qsh cmd('python3 script.py "with space" ''with"dblquote''')
For better readability:
qsh cmd(^python3 script.py #with space# ^^with#dblquote^^^)
So it seems that you can use either single- or double-quotes as
parameter delimiters, whichever is more convenient. (In this case, it
will probably be double-quotes, because they are just one character
each inside the single-quotes that delimit the entire command.)
But now, what about the (rare but possible) case where you want to
pass a string that contains both a single-quote and a double-quote?
Are we just out of luck?
(I'm guessing yes.)
John Y.
As an Amazon Associate we earn from qualifying purchases.