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

Re: Can zsh `else` reserved keyword command be aliased and the lexem itself be repurposed as `fi` keyword command?

On Thu, Dec 28, 2017 at 7:07 AM, mathieu stumpf guntz
<mathieu.stumpf-guntz@xxxxxxxxxx> wrote:
> it might be expected that a command `keyword` equivalent of what `builtin`
> and `command` are doing for their respective eponymous token category

If you haven't already, go read https://askubuntu.com/a/590335 which
was linked from

Your hypothetical "keyword" would have to itself be a keyword (and
thus not a command).

> This problem was found in the following tricky scenario: being able to
> replace `else` with `alie` and `fi` with `else`.
>   alias alie="keyword else"
>   alias else='fi'

What's the underlying reason for doing this?  That is, aside from the
overall desire to write in Esperanto (?),  I get wanting "alie" to
mean the same thing "else" means, but not why "else" should mean "fi".
Even in your "alie echo 'mirinda mondo'"  example it looks like "else"
is meant to act as "fi;else" so that you can continue with more of the

> This is not yet a working solution, but here is an idea: using the `-r` flag
> of `enable` and `disable` builtin commands to change visibility of the
> `else` keyword

This is doomed to failure, because complex shell syntax is fully
parsed before it is executed.  Your "disable" or "enable" has to take
place before the parsing step, because it won't be executed during the
parse.  Some extremely simple cases might appear to work at the
command prompt, but as soon as you embed them in a larger structure
like a function body they will fail.

In fact the "fully parsed before executed" property is one of the
important reasons for keywords being distinct from commands.

Aliases "work" before the parse because they are merely text
replacements and can be done before full token analysis is performed.

What is needed is an alias that ends recursive replacement.  Normally
one does this by including quoting (e.g., a leading backslash) in the
value of the alias, but that doesn't help in this case because it
prevents the replacement from being considered a keyword as well.
There may be a clever way to accomplish this -- recursion stops if
replacement results in a previously replaced alias, for example, to
avoid infinite loop -- but I haven't worked out an answer.

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