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

Re: A couple of bugs in zsh-3.0.7

James Antill wrote:
>% alias abcd="echo abcd"
>% abcd () { echo xyz  
>2> }
> /* running abcd at this point crashes zsh */

This is not a bug.  It's a rather unintuitive, but standard, interaction
between aliases and functions.  The word "abcd", when it is defined as
an alias, gets expanded anywhere that it appears in a command position.
The catch is that in the traditional function definition syntax, the name
of the function being defined is in command position.  So the alias gets
expanded, and you end up doing

	echo abcd () { echo xyz; }

The next catch is that this defines *both* "echo" and "abcd" as functions,
with the body you gave.  "echo" is a function that calls itself.
When you run the "abcd" function, it calls the "echo" function, which
calls itself recursively until the stack overflows.

There are three ways to avoid this.  Firstly, don't mix aliases and
functions, or at least define all functions before aliases.  Secondly,
escape the command word when defining a function, to prevent it being
expanded as an alias.  Finally, you can use the ksh function definition

	function abcd { echo xyz; }

which doesn't expand the function name as an alias.


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