zsh-users
 help / color / mirror / code / Atom feed
* When it is allowed to use alias after defining it?
@ 2022-12-30 21:45 Sebastian Gniazdowski
  2022-12-30 22:01 ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Sebastian Gniazdowski @ 2022-12-30 21:45 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 423 bytes --]

I was thinking that in current file it is impossible to use an alias – only
should work in sourced sub-script.

So:
alias q=pushd
# Expected to not work
q
# Expected to work
source ./using-q.zsh

The script is  fine to use q. Is that the truth? Because I'm using aliases
in the same file, only if it is at some proximity from definition. What are
the rules here?

-- 
Best regards,
Sebastian Gniazdowski

[-- Attachment #2: Type: text/html, Size: 1363 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: When it is allowed to use alias after defining it?
  2022-12-30 21:45 When it is allowed to use alias after defining it? Sebastian Gniazdowski
@ 2022-12-30 22:01 ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2022-12-30 22:01 UTC (permalink / raw)
  To: zsh-users

On Fri, 2022-12-30 at 21:45 +0000, Sebastian Gniazdowski wrote:
> I was thinking that in current file it is impossible to use an alias – only should work in sourced sub-script. 
> 
> So:
> alias q=pushd
> # Expected to not work
> q
> # Expected to work
> source ./using-q.zsh
> 
> The script is fine to use q. Is that the truth? Because I'm using
> aliases in the same file, only if it is at some proximity from
> definition. What are the rules here?

It's a question of when the execution of the line happens in relation to
parsing it, and also of the fact that expanding an alias happens very
early at the point where the line is read in.

When reading a script, the shell reads one line at a time --- if the
line contains an unfinished complex command, it will continue reading
until that's finished.  Then it executes it, before reading anything
else, so if the line was an alias definition that's available from then
on.  That's pretty much the same as what happens in an interactive
shell, in fact it's the same internal logic --- the difference between
the two occurs at the point where the shell gets input, where it decides
whether it's blindly going to read a line or invoke the editor.

So complex commands are the key case where the effect is delayed.  This
obviously includes functions; the classic case where you *can't* rely on
an alias being defined is:

fn() {
  alias q=pusehd
  q
}

since the function is defined in one go before the alias is defined
within the function.  Because aliases are resolved on input, the q is
treated as a normal command within the function.

Although this is a logical consequence of shell input, it's certainly
not an obvious one.

pws



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-12-30 22:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-30 21:45 When it is allowed to use alias after defining it? Sebastian Gniazdowski
2022-12-30 22:01 ` Peter Stephenson

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).