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

Re: Alias call in function fails...



On Mon, Jun 29, 2020 at 9:26 AM Sebastian Gniazdowski
<sgniazdowski@xxxxxxxxx> wrote:
>
> I once was testing with Eric on IRC and the aliases sometimes do work
> within the same file. I think that the distance between the definition and
> usage is important.

No, that's unrelated.  Code structure and execution context are
everything.  As Peter explained, initialization files, sourced files,
and scripts (that are not function bodies) are executed one
newline-separated command at a time (Peter said "line by line" but
that's not precisely correct **), whereas function definitions (no
matter where they appear, but especially autoloads) are fully parsed
before being executed.  If you think about it, that latter must be
true, otherwise you'd run the function instantly every time you tried
to define one.

Where you're probably becoming confused is "one newline-separated
command at a time".  Pipelines, commands joined with ";" or "&&" or
"||", and structured commands such as an if/else cascade or a while
loop, are all, like a function body, fully parsed before being
executed.  Even a sequence of commands simply enclosed in braces is
fully parsed before being executed.  Thus even in an interactive
shell:

% { alias -g foo=bar
cursh> echo foo }
foo
% echo foo
bar
% alias -g foo=again ; echo foo
bar
% echo foo
again

You have to be certain that the alias command is actually executed,
NOT merely seen by the parser, before the first usage of that alias is
seen by the parser.

Run your confusing script with "setopt xtrace" and watch for the order
of execution.



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