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

Re: outputting quotes in a command

Eric Smith wrote:
> I want to output from a script the following command
> (obviously where I will have foo bar as variables).
>  $  mutt -f foo -e 'push "<limit> ~f bar ~d <2w^M"'
> Where ^M represents a carriage return

(I presume you're outputting to a file, otherwise the carriage return
will cause the last couple of characters to be output at the beginning
of the line.)

There are various ways. One way is to avoid having to
quote the form of quotation you're using at that point:

print "mutt -f foo -e 'push "'"<limit> ~f bar ~d <2w\r"'"'"

although it might be better with a bit of backslashing just to avoid
confusingly swapping quotes:

print "mutt -f foo -e 'push \"<limit> ~f bar ~d <2w\r\"'"

If foo and bar can themselves include arbitrary characters, the best
thing to do is use the parameter substitution flags to quote them, and
use raw print while getting $'...' to do the work of the carriage return:

print -r "mutt -f" ${(q):-foo} -e "'push \"<limit> ~f" ${(q):-bar} \
  $'~d <2w\r"\''

(I've done that rather pedantically by quoting a literal foo or bar to
get what I had before; omit the ":-"s when you have real variables.)

However, assuming the line you've output is to be reinterpreted by zsh,
it might well be better still to output the code to output a carriage
return, if you see what I mean, rather than have it raw in the file:

print -r "mutt -f" ${(q):-foo} -e "'push \"<limit> ~f" ${(q):-bar} \
  "~d <2w'\$'\\r\"'"

That's really quite hairy:  you're outputting

mutt -f foo -e 'push "<limit> ~f bar ~d <2w'$'\r"'

in which the \r is interpreted when the line is evaluated by the shell,
so that the final argument appears as the single string

push "<limit> ~f bar ~d <2w^M"

where ^M is now a literal carriage return.  I think.

Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070

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