zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-users@zsh.org
Subject: Re: Please help me quote ! :)
Date: Mon, 26 Feb 2018 20:37:36 +0000	[thread overview]
Message-ID: <20180226203736.0d5f92ff@ntlworld.com> (raw)
In-Reply-To: <20180226175037.hgrrnft44wjx3so7@solfire>

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


      parent reply	other threads:[~2018-02-26 20:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-26 17:50 tuxic
2018-02-26 20:36 ` Martijn Dekker
2018-02-26 20:37 ` Peter Stephenson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180226203736.0d5f92ff@ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).