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

Re: quoting arguments in a script



On 2008-10-14 at 21:28 +0200, denis wrote:
> I'm looking for a way to quote strings in a zsh script.
> 
> Consider the following example script which should be able to be
> used exactly like the original grep:
> 
>   #!/bin/zsh
>   emacsclient -e "(grep \"grep -nH $@\")"
> 
> This is not working because $@ breaks the string.  Replacing $@
> with $* won't be enough either.
> 
> Maybe it is possible to use the builtin function quote-line (M-')
> for this?

I don't know exactly what syntax you need, not knowing the other tools,
but you should probably be looking at something like:

  ${(q)^^@}

Or use  ${(qq)^^@} if you want to see the results always quoted.  The
(q), (qq), etc modifiers are documented in zshexpn(1) and quote the
results.  The ^^ is to explicitly turn off rc_expand_param; you're not
running "#!/bin/zsh -f" so start-up files are an issue.

% function foo { print "foo ${(qq)^^@}" }
% foo a b c
foo 'a' 'b' 'c'

The single (q) should be sufficient.

To highlight the ^^, for me with rc_expand_param set, I get the same
results that you'd get if you used ^ (explicitly turn on; doubling is
explicit off instead of accepting default; this too is documented in
zshexpn(1) but it's something you need to know that you need to look
for).

% function foo { print "foo ${(qq)^@}" }
% foo a b c                             
foo 'a' foo 'b' foo 'c'

Regards,
-Phil



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