zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: "zsh-users@zsh.org" <zsh-users@zsh.org>
Cc: Frank Gallacher <franxg@gmail.com>
Subject: Re: Alias call in function fails...
Date: Mon, 29 Jun 2020 09:55:21 -0700	[thread overview]
Message-ID: <CAH+w=7bKwEJYK_+a6p9ZzngPFpqPjJ+kojFUKckurr5b9_YS9Q@mail.gmail.com> (raw)
In-Reply-To: <CAKc7PVB_0V=f4UCmbOeb=WvR9ewdUvNW0PtrGGOdJFvBHq_3Pw@mail.gmail.com>

On Mon, Jun 29, 2020 at 9:26 AM Sebastian Gniazdowski
<sgniazdowski@gmail.com> 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.

  reply	other threads:[~2020-06-29 16:56 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-22 12:49 Frank Gallacher
2020-06-22 23:23 ` Mikael Magnusson
2020-06-23  8:37   ` Andreas Kusalananda Kähäri
2020-06-23  9:14     ` Peter Stephenson
2020-06-23  9:55     ` Mikael Magnusson
2020-06-23 11:28       ` Andreas Kusalananda Kähäri
2020-06-23 12:04         ` Daniel Shahaf
2020-06-23 12:46           ` Perry Smith
2020-06-23 13:10             ` Kamil Dudka
2020-06-23 16:03               ` Bart Schaefer
2020-06-23 21:14                 ` Perry Smith
2020-06-23 22:54                   ` Daniel Shahaf
2020-06-23 23:29                     ` Perry Smith
2020-06-23 23:43                       ` Bart Schaefer
2020-06-24  0:47                         ` Perry Smith
2020-06-24  9:28                           ` Daniel Shahaf
2020-06-24 12:55                             ` Perry Smith
2020-06-23 23:40                     ` Bart Schaefer
2020-06-24 10:10                       ` Daniel Shahaf
2020-06-24 10:47                         ` Daniel Shahaf
2020-06-24  2:58                   ` Grant Taylor
2020-06-29 16:24   ` Sebastian Gniazdowski
2020-06-29 16:55     ` Bart Schaefer [this message]
2020-06-30  4:02       ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7bKwEJYK_+a6p9ZzngPFpqPjJ+kojFUKckurr5b9_YS9Q@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=franxg@gmail.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).