zsh: Aliasing

Next: Quoting, Previous: Comments, Up: Shell Grammar [Contents][Index]

### 6.8 Aliasing

Every eligible word in the shell input is checked to see if there is an alias defined for it. If so, it is replaced by the text of the alias if it is in command position (if it could be the first word of a simple command), or if the alias is global. If the replacement text ends with a space, the next word in the shell input is always eligible for purposes of alias expansion. An alias is defined using the `alias` builtin; global aliases may be defined using the `-g` option to that builtin.

A word is defined as:

- Any plain string or glob pattern
- Any quoted string, using any quoting method (note that the quotes must be part of the alias definition for this to be eligible)
- Any parameter reference or command substitution
- Any series of the foregoing, concatenated without whitespace or other tokens between them
- Any reserved word (`case`, `do`, `else`, etc.)
- With global aliasing, any command separator, any redirection operator, and ‘`(`’ or ‘`)`’ when not part of a glob pattern

It is not presently possible to alias the ‘`((`’ token that introduces arithmetic expressions, because until a full statement has been parsed, it cannot be distinguished from two consecutive ‘`(`’ tokens introducing nested subshells.

When `POSIX_ALIASES` is set, only plain unquoted strings are eligible for aliasing. The `alias` builtin does not reject ineligible aliases, but they are not expanded.

Alias expansion is done on the shell input before any other expansion except history expansion. Therefore, if an alias is defined for the word `foo`, alias expansion may be avoided by quoting part of the word, e.g. `\foo`. Any form of quoting works, although there is nothing to prevent an alias being defined for the quoted form such as `\foo` as well. Also, if a separator such as `&&` is aliased, `\&&` turns into the two tokens `\&` and `&`, each of which may have been aliased separately. Similarly for `\<<`, `\>|`, etc.

For use with completion, which would remove an initial backslash followed by a character that isn’t special, it may be more convenient to quote the word by starting with a single quote, i.e. `'foo`; completion will automatically add the trailing single quote.

There is a commonly encountered problem with aliases illustrated by the following code:

    alias echobar='echo bar'; echobar

This prints a message that the command `echobar` could not be found. This happens because aliases are expanded when the code is read in; the entire line is read in one go, so that when `echobar` is executed it is too late to expand the newly defined alias. This is often a problem in shell scripts, functions, and code executed with ‘`source`’ or ‘`.`’. Consequently, use of functions rather than aliases is recommended in non-interactive code.

Note also the unhelpful interaction of aliases and function definitions:

    alias func='noglob func'
    func() {
        echo Do something with $*
    }

Because aliases are expanded in function definitions, this causes the following command to be executed:

    noglob func() {
        echo Do something with $*
    }

which defines `noglob` as well as `func` as functions with the body given. To avoid this, either quote the name `func` or use the alternative function definition form ‘`function func`’. Ensuring the alias is defined after the function works but is problematic if the code fragment might be re-executed.

Next: Quoting, Previous: Comments, Up: Shell Grammar [Contents][Index]