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 ''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 ^^with space^^ ^^with^^^^quote^^^)

The literal space in the first parameter came through fine. But the
second parameter actually became


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:

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

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 "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 #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 "with space" ''with"dblquote''')

For better readability:

qsh cmd(^python3 #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.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2022 by and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.