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

Re: here document within a switch fails to parse.

> On 08 January 2021 at 17:13 Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
> function test1 ()
> {
> :<<EOC
> $(print something from a function with dollarsign)
> print something from a function redirected > /dev/stderr
> $(print something from a function both > /dev/stderr)       # This is 
> the only thing that prints.
> print something from a function
> COMMENT from a function
> echo "echo from a function"
> }
> ... not to belabor it, but why in heaven is just that one construction 
> evaluated?

You have what is effectively a double quoted expression.  The shell
evaluates any substitutions it needs to get the arguments to the
command, so it can execute it.  It needs to do that so it can end
up with a simple set of strings, "command", "command argument 1",
and so on.  At this point it doesn't care what the command actually
is.  (In particular, it does NOT think "oh, this is Ray abusing
the ":" command, so I'd better not do my usual processing".  Nobody
has got around to adding that mode to the shell yet.)

So how it does it get "command argument 1" from the expression
you've passed to it?  There are only actually two subsitutions
in that expression to be evaluated, the two $(...) expressions;
everything else will be passed to the command as is.  That
includes anything that happens to look like a command ---
remember, it's just stuff in a double quoted string at this point.

The first $(...) is evaluated.  It has no redirection, so that
argument to the "print" is substituted into the string.  You
don't actually see that, it's just part of the command argument

The second $(...) is evaluated.  This time, there's a redirection,
so the argument gets sent to stderr, so you see it.  There's
no output to stdout, so what will actually be substituted into
the argument on the command line is the empty string.


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