zsh-users
 help / color / mirror / code / Atom feed
* Please help me quote ! :)
@ 2018-02-26 17:50 tuxic
  2018-02-26 20:36 ` Martijn Dekker
  2018-02-26 20:37 ` Peter Stephenson
  0 siblings, 2 replies; 3+ messages in thread
From: tuxic @ 2018-02-26 17:50 UTC (permalink / raw)
  To: zsh-users

Hi,

is there a way to automatically escape all "bad" characters
(like spaces) while expanding a shell variable containing a
"bad" filename?

Thanks a lot for any help!
Cheers!

Meino


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Please help me quote ! :)
  2018-02-26 17:50 Please help me quote ! :) tuxic
@ 2018-02-26 20:36 ` Martijn Dekker
  2018-02-26 20:37 ` Peter Stephenson
  1 sibling, 0 replies; 3+ messages in thread
From: Martijn Dekker @ 2018-02-26 20:36 UTC (permalink / raw)
  To: zsh-users

Op 26-02-18 om 17:50 schreef tuxic@posteo.de:
> is there a way to automatically escape all "bad" characters
> (like spaces) while expanding a shell variable containing a
> "bad" filename?

Yes. See 'man zshexpn' under the heading "Parameter Expansion Flags".
The flag you need is the "q" flag. For instance, instead of $var, use
${(q)var}. See the man page for how to tweak the quoting format.

HTH,

- M.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Please help me quote ! :)
  2018-02-26 17:50 Please help me quote ! :) tuxic
  2018-02-26 20:36 ` Martijn Dekker
@ 2018-02-26 20:37 ` Peter Stephenson
  1 sibling, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2018-02-26 20:37 UTC (permalink / raw)
  To: zsh-users

On Mon, 26 Feb 2018 18:50:38 +0100
tuxic@posteo.de wrote:
> is there a way to automatically escape all "bad" characters
> (like spaces) while expanding a shell variable containing a
> "bad" filename?

I assume the intention is so that anything that would be special
to the shell is hidden away from immediate expansion, but in such a away
that when you output the result later you get back the original file name.

It's easy using the (q) family of parameter flags.  So, for example,

% touch 'space file'
% files=(*)
% print -r $files
space file
% print -r ${(q)files}
space\ file

I presume that output (the -r stops print from swallowing the backslash)
is the sort of thing you're after.  (q) is smart about what needs
quoting.  (qq), (qqq), (qqqq), (q-) and (q+) are only cosmetically
different: they have the same practical effect unless you want the input
to be evaluated by a different shell.

The quoting is by word, so if there were more matches in $files, all the
file names get that behaviour separately (they aren't joined by
quoted spaces).

However, with native zsh options, you only need this if you're going to
use the variable in some context where the value is going to be expanded
again, like an "eval."  Otherwise, the fact that zsh leaves the contents
of variables well alone if there substituted back onto the command line,
unlike other shells where you need double quotes,  means that just using
the variable itself --- $files here --- will work most of the time.

So if thisn't quite what you want, you'll need to tell us what you're
actually doing (usually a good plan anyway).

pws


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-02-26 20:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-26 17:50 Please help me quote ! :) tuxic
2018-02-26 20:36 ` Martijn Dekker
2018-02-26 20:37 ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).