Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: there should be a way to echo with quotes or escaping



On Mar 23,  6:03pm, Dave Yost wrote:
} Subject: Re: there should be a way to echo with quotes or escaping
}
} This should be documented.

It is.  It's under "parameter expansion", along with all the other
expansion flags.

} and there should be some mention under the echo command.

Why?  It has nothing to do with the echo command, or with any other
particular command for that matter.  You can use parameter expansion
anywhere.

} These searches in zshall come up with nothing:
}   \${\(q
}   qqq

The first of those depends on finding a literal example, and the second
assumes that "qqq" would be documented separately from "q" by itself,
which it happens not to be.  Did you happen to think of searching for
the word "quote" all by istelf?

man zshall | grep -i quote

produces 159 lines of output among which is

       q      Quote the resulting words with  backslashes.  If  this  flag  is
              given twice, the resulting words are quoted in single quotes and
              if it is given three times,  the  words  are  quoted  in  double
              quotes.  If it is given four times, the words are quoted in sin-
              gle quotes preceded by a $.

It mentions "quote" so many times, grep extracts the entire paragraph!

}   rc_quotes

This really has nothing to do with it, either, but if you had used
"grep -i" you'd have found it.  (Well, actually, you might not, because
it's rendered in boldface which "man" emits as backspace-overstriking,
so you have to put "man" through something that undoes the overstrike
before you grep it.  If you want a searchable format, don't use "man".)

} Furthermore, none of these three choices is best.  Forgetting the
} rc_quotes option, which is for aliens, they all resort to backslash
} for shell metacharacters like $ ` etc.

"Best" by what definition?

The point is that you specify what outermost quotes you want used, so
that you know what to expect when you embed the expansion another
context.  The problem with "quote like a human" is that zsh can't know
what the surrounding context will be when the expansion finishes, so
it doesn't know how to begin or end.

I wrote some code for zmail, years ago, that attempted to do "quote
like a human" and the border cases (where the string to be quoted itself
ends in a quote, for example) are a disaster.  Most of the time it works
like you seem to think would be "best" but occasionally you get insane
runs of '"'"'" that it's impossible to decipher.  The occasional back-
slash you get with ${(qq)...} really is much preferable.



Messages sorted by: Reverse Date, Date, Thread, Author