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

Re: protect spaces and/or globs



> On Feb 11, 2021, at 11:31 AM, Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
> 
>   while [[ $# -gt 0 ]] ; do
>     arg="$1"
>     shift
>       grepargs+=( "$arg" )
>   done

Why are you still accreting grepargs like this? Peter has already
shown you how to do this.

	grepargs=("$@")

Unless your actual code actually modifies the args before adding
them to the array?

> Seems cleaner, the single quotes are themselves protected or replaced so that
> it not only runs right, it looks right; on recall it is  visually exactly the same.

The command saved to history is correct, but it is not always exactly
what you entered.

	% grep_wrapper on\ the\ current i,2,light\ edit i,1,old\ stable 

	what should be going to history:
	grep --color=always -i -- 'on the current' 'i,2,light edit' 'i,1,old stable'

	[...]

	% pat='on the current' foo='i,2,light edit' bar='i,1,old stable'
	% grep_wrapper $pat $foo $bar

	what should be going to history:
	grep --color=always -i -- 'on the current' 'i,2,light edit' 'i,1,old stable'

	[...]

A ${(q-)foo} expansion basically re-quotes the value of foo so
it works correctly with eval, in whatever way is requested. It
doesn't know the value's origins.

> And is it not intuitive that, since single quotes already
> protect their string, if you add protection for the single
> quotes themselves, you then have a perfectly protected string?

That's not how quoting works. Quote levels don't nest to produce
some kind of super-quoting. You're misinterpreting the results of
your experimentation.

	% foo='a b c'
	% print -r 'protected: $foo'
	protected: $foo
	% print -r "'not protected: $foo'"
	'not protected: a b c'

The double quotes do protect the single quotes from being interpreted
by the shell, but they also permit expansions.

vq



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