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

Re: activate alias inside subshell

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


% 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

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