zsh-workers
 help / color / mirror / code / Atom feed
* Problem with alias in 'if' blocks
@ 1997-01-04  1:44 Brady Morgan
  1997-01-04 19:51 ` Zefram
  0 siblings, 1 reply; 2+ messages in thread
From: Brady Morgan @ 1997-01-04  1:44 UTC (permalink / raw)
  To: zsh-workers

I attempting to convert from Csh to Zsh and have run into the following
problem.

I've isolated the problem to the following script, "simple":
 (NOTE: this listing is shown with line numbers obtained using "cat -n")

     1	#!/usr/local/bin/zsh
     2	
     3	if [ 1 ]; then
     4	    alias mycmd='echo "MYCMD ALIAS"'
     5	
     6	    if [ 1 ]; then
     7		mycmd
     8	    fi
     9	
    10	    mycmd
    11	fi
    12	
    13	mycmd

When executed, I get this result:

simple: command not found: mycmd [7]
simple: command not found: mycmd [10]
MYCMD ALIAS


Can someone explain why the alias isn't "known" until I'm out of the
"if" loop?  Also, if I've goofed with this, could someone demonstrate
the proper way to achieve the desired result.

This script works with Csh (i.e., when it's in Csh syntax).

I checked the Zsh documentation, the Zsh web site, and the DejaNews
news archives, but couldn't find anything regarding this issue.

Here's my setup:
	OS	   : SunOS 4.1.3_U1  sun4m
	Zsh Version: 3.0.2
	Compiler   : gcc version 2.7.2

Any help will be greatly appreciated!!


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

* Re: Problem with alias in 'if' blocks
  1997-01-04  1:44 Problem with alias in 'if' blocks Brady Morgan
@ 1997-01-04 19:51 ` Zefram
  0 siblings, 0 replies; 2+ messages in thread
From: Zefram @ 1997-01-04 19:51 UTC (permalink / raw)
  To: Brady Morgan; +Cc: zsh-workers

Brady Morgan wrote:
>Can someone explain why the alias isn't "known" until I'm out of the
>"if" loop?  Also, if I've goofed with this, could someone demonstrate
>the proper way to achieve the desired result.

Aliases are expanded when commands are first scanned, not when they are
executed.  The entire if construct has to be read in and parsed before
it can be executed at all, so the aliases expanded in it are those in
effect at the start of the construct.

The solution is to use a shell function, rather than an alias.  The
behaviour of functions is better defined, and more like normal
commands.  Aliases are really only useful interactively.

-zefram


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

end of thread, other threads:[~1997-01-04 19:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-01-04  1:44 Problem with alias in 'if' blocks Brady Morgan
1997-01-04 19:51 ` Zefram

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