Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Comment (# char) behavior in the sub-shell
- X-seq: zsh-workers 41663
- From: Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: Re: Comment (# char) behavior in the sub-shell
- Date: Sun, 10 Sep 2017 21:06:36 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ntlworld.com; s=meg.feb2017; t=1505073997; bh=ugiLOcYYJx3DEsh4YmqRQqRrW0V8w4D0ibzI9VbaNBk=; h=Date:From:To:Subject:In-Reply-To:References; b=iv9alNvP5II0Mrtzn3/Jduf1PINqdJGOZjPTtxvUaTGQfB1Sc35Vegx/HDzYabFYv VPVKZwyPeaE5gRdHIss7+4ZwKLXTw+4dyRL2Swkj6rsY9kYXx7FT51oYESKc1v9pCC JhJKtvg3ekfef7pKx4FXHIzd26NrH77b2+p8qUwIGdE9KLHlzgGZqIL1u33WZIl1RD R8LCEv5TrpEvTg1KQUed+GPiBsIk3AlnX0uQyYDeQWYifBHUUdmerHFRO4Fc3bdRkh viQIELQoKceHhuv9oGv2upV8pPEBZuq0PrWO9m10go+XRh+DCgx3OrLIEMEs6HGj90 PJ8sV8HqhU43Q==
- In-reply-to: <CAAR+aWfD1ifEe2JGxJAkgA5Rmpr6nmV9Ta9Y6yvW23-oe4hkYg@mail.gmail.com>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <CAAR+aWfD1ifEe2JGxJAkgA5Rmpr6nmV9Ta9Y6yvW23-oe4hkYg@mail.gmail.com>
On Sat, 9 Sep 2017 19:33:59 +0300
Stanislav Seletskiy <s.seletskiy@xxxxxxxxx> wrote:
> zsh version: 5.4.2
>
> I'm heavy user of `#`-aliases, so I like to do stuff like `seq 1 10 #
> 9` to grep for `9` (`alias -g -- '#'='| grep'`).
>
> It works as expected when I'm entering command in interactive mode
> without sub-shell, like:
>
> `$ seq 1 10 # 9` — works well, I see only `9` in the output.
>
> But when I'm trying to use same alias in the sub-shell (e.g. inside
> `$()`), it doesn't work anymore:
>
> `$ echo $(seq 1 10 # 9)` — doesn't work, I see `1 2 3 ... 10` in the output.
I suspect this changed some versions ago now. At some point we changed
the way we handled $(...) to parse it better. Previously, we just
blindly treated it as a string when we first enountered it, reading it
in interactively until we got to a closing parenthesis. That wasn't
actually the right thing to do, and crucially it failed where there was
a case statement in the old syntax where a pattern only has a closing
parenthesis and not an opening parenthesis. So what we do now is parse
properly until the (right) closing parenthesis, but still store what we
parsed as a string for reading again when we execute the $(...).
It so happens we don't keep the expanded aliases from the original parse
in the string we later replay. There is a test for just this case;
here it is:
alias fooalias=barexpansion
funcwithalias() { echo $(fooalias); }
functions funcwithalias
barexpansion() { print This is the correct output.; }
funcwithalias
0:Alias expanded in command substitution does not appear expanded in text
>funcwithalias () {
> echo $(fooalias)
>}
>This is the correct output.
You'll see that the output from "functions" shown at the end contains
the unexpanded alias (that "0:..." in the middle simply gives the
expected exit status for the code above and a description). In your
case, that would be the "#", and at the point where it finally does get
read in, in recent shell versions, it's treated like a string, not
interactive input. If we kept expanded aliases the definition of
functionwithalias would change correspondingly. Actually, there is an
argument for that since since that's what would happen for an alias in
the main body of the function, but I'm not sure how close the parallels
are
I'm not sure how much effort this is worth.
pws
Messages sorted by:
Reverse Date,
Date,
Thread,
Author