Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Please help me quote ! :)
- X-seq: zsh-users 23178
- From: Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: Please help me quote ! :)
- Date: Mon, 26 Feb 2018 20:37:36 +0000
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ntlworld.com; s=meg.feb2017; t=1519677457; bh=j2icodfVqiGxzsZhlxH0zLVxV8qG3if3HfWb/aSVo1U=; h=Date:From:To:Subject:In-Reply-To:References; b=vFB8NoS6/uZt4Bfm7Ej11FvTzdurRNip7yQOVXbd8jvrcMfBuYaagedbjCXQk8QiB wGdCDfHoyKmnqR8YK9lY9E3N+7G9aOZkUdcFRFTve2XUt6vEmQ3495hEpijuDfTcyV bhnDrNvg2WQO7WEE33PRbVvxGU72d07LZvf20+q+PfASLIXZ0NZ+2AhLS3qU59SY+S u88qHxy+E1g0uWgQR0mpJHra5MmBWe6PDDrinvYh91VdyXxvegG/IavNUddVE5OgVO ozsRBKXJSw4Pd4Lp9F8Scu4GTc7Lz1p0gXjTeh8xWSX+plnnGYCCGlomJDZxFo+sLk 2gWC3u5VPgPwg==
- In-reply-to: <20180226175037.hgrrnft44wjx3so7@solfire>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- List-unsubscribe: <mailto:zsh-users-unsubscribe@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <20180226175037.hgrrnft44wjx3so7@solfire>
On Mon, 26 Feb 2018 18:50:38 +0100
tuxic@xxxxxxxxx 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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author