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

Re: About quoting



Ra?l N??ez de Arenas Coronado wrote:
>    $ export PAGER="/my/pager --flag --otherflag -"
>    $ ls | $PAGER
>
>    The matter here, I think, is that spaces remains quoted when
>doing the expansion in the redirection.

You're correct in identifying the effect, but the cause isn't quite
what you think.  The quoting isn't relevant here, it's not part of the
variable's value.  What's actually happening is that zsh simply isn't
dividing up the expanded value at spaces, which bash does.

There are several solutions.  If you want to divide up a scalar value
in that way, add an "=" after the "$", so

	$ ls | $=PAGER

will do what you want.  You can also set the option SH_WORD_SPLIT, which
does this automatically for all variable substitutions, but this is a bad
idea, because it's often useful to retain spaces in a variable expansion.
(This option is turned on automatically if zsh is invoked under the name
"bash", "sh", or "ksh", for compatibility.)

Another way to do what you want is to define an array variable, which
will be substituted as separate words:

	$ pager=(/my/pager --flag --otherflag -)
	$ ls | $pager

This has the advantage that the words can contain spaces.  However,
arrays cannot be exported.

For the use you're making of the variable here, the proper solution is
actually a shell function:

	$ function pager {
	>   /my/pager --flag --otherflag -
	> }
	$ ls | pager

If you want to use the standard $PAGER variable from within the shell,
with it set the way you describe, I think the neatest solution is to
hide the $=PAGER expansion in a shell function:

	$ export PAGER="/my/pager --flag --otherflag -"
	$ function pager {
	>   $=PAGER
	> }
	$ ls | pager

But you should also consider that some programs that use $PAGER won't
interpret it the way you intend.  You'd be better off putting the complex
command into a shell script, and putting the path of that script into
$PAGER.

-zefram



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