Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: activate alias inside subshell
- X-seq: zsh-users 23318
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: activate alias inside subshell
- Date: Sun, 8 Apr 2018 00:39:39 -0700
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=brasslantern-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=QlHGavNPglgVC0N99vYXZX+2nOWnXqX5xWs0V8sTfrI=; b=eD34bxbWgplLO46i3KgwwtuxQdRILvxI6WxzRnbsCY9Svot35d6ejWhM3LtZsmwVkk OV1CD89+t9G6ThkkD/ee6+WGcsFQyF4wCNzCYMCVywqf0CkD2ZGx1uDAOmtO7a5oJrQ5 lfuGO6ndOREd4f7C2tyrse7MRTJwmcyP0++EjATxSt+gWNgvfEX2vz7XLvHul1s7WXRg kE/ELjkz254NSi9Qrf7aDS6OSebipyf+NZ4XJqUZvNhA+xR0ZRefMtVeekRKGObvE/0p d62MoSURDef7rHKTQQOSLA/g1Q328TGqu1kVFONkDh9lFNufoYIacseKgTLCD4G6JcrM BV8g==
- In-reply-to: <637434d9-ff23-53d4-5d31-443942fc2ef9@eastlink.ca>
- 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: <604319cb-d86f-686b-ac9b-00d21650edff@eastlink.ca> <637434d9-ff23-53d4-5d31-443942fc2ef9@eastlink.ca>
I'm not going to try to respond blow-by-blow to your messages, because
there still seem to be some concepts about aliasing that we've
discussed in past threads that you don't appear to be applying.
Firstly, though, there's something that doesn't add up about your
"test1" example; as you quoted it, "yelline" references a variable
$yel that's never assigned, and if I attempt to replicate verbatim in
a freshly-started shell what you say you've done, I get exactly the
errors I expected:
% . test1; test1
test1:10: defining function based on alias `msg'
test1:14: parse error near `()'
zsh: command not found: test1
I suspect you're using a shell in which you've been running other
tests and something from an earlier experiment is spilling over to
confuse you. Either that, or you haven't actually shown us the full
contents of the "test1" file.
The fundamental thing you're forgetting is that aliases act by text
replacement. "whence" will tell you whether an alias WILL replace
text the NEXT time the lexer is invoked, but that has no meaning for
text that has previously been parsed. Function definitions are always
fully lexed+parsed before they are executed, so once a function is
defined any aliases it may have used have been replaced by the
expanded text of the alias and can never expand again.
We explained this several threads ago when you were shown the "eval"
example. "eval" runs the parser again so aliases have a chance to
expand.
Thus:
% alias msg='echo this is an alias'
% test1() { msg in test1 }
% functions test1
test1 () {
echo this is an alias in test1
}
%
See how the use of the alias is no longer present in the stored
definition of test1? Note that it's important that the alias and the
function were defined separately (in this example at separate PS1
prompts), so that the parser was run a second time AFTER the alias was
defined.
Compare running the parser only once, by using a multi-line construct
(I have started a fresh "zsh -f" for each of these examples even
though I've changed the function names to differentiate them):
% {
cursh> alias msg='echo this is an alias'
cursh> test2() { msg in test2 }
cursh> }
% functions test2
test2 () {
msg in test2
}
% test2
test2: command not found: msg
%
Here test2 was parsed at the same time as the alias command, so the
alias was not yet "active" when the body of test2 was read, and thus
the alias definition is both not expanded in test2 and also irrelevant
at the time the function is executed.
However, that has no bearing on what "whence" will say about the
current state of the alias at the time the function is executed:
% {
cursh> alias msg='echo this is an alias'
cursh> test3() { whence -v msg; msg }
cursh> }
% test3
msg is an alias for echo this is an alias
test3: command not found: msg
%
Something like this must be involved in producing the "nulled" output
from the example you posted.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author