Skip to content

alias / unalias

Description

The alias command creates a snippet or macro that gets interpolated into your command by the shell. For example, you can create an alias that adds the -ld parameters to the ls command, so it runs ls -ld when you issue just the ls command.

To demonstrate, I replaced the builtin ls command with gls an ls replacement, to get prettier output. To ensure that it colorizes the output, I have created an alias to the ls command that includes the --color=tty parameter, so I get gls color output every time I run ls. Looks a little like this

» alias ls
ls='gls --color=tty'
» ls /usr/local/bin/python
---> ACTUALLY RUNS THIS: gls '--color=tty' /usr/local/bin/python
Resulting in this colorized output: images/img.png

Tip

Do this first: read the docs on the alias command for the relevant shell. Zsh docs are clear and precise. See that and others in the See also section, below.

It's Not a Shell Script

Aliases are fast and convenient and should be one of the tools at in your toolbox, but they have a pretty specific use-case, which is command-parameter substitution. If you need anything more than that, use a shell script.

They're powerful, but not flexible. They only replace text text substitution, they take no parameters, and provide no other functionality. They replace alias names with values, so they're definitely

Tip

For anything besides simple text-replacement functionality, use a shell script. If it requires variables or would be simpler and/or more elegant using them, make it a shell script.

Given that admonition, aliases can be the perfect tool for the job. My aliases file is about 200 lines long. I've been dragging that thing around forever, as evidenced by my handy Perl hack, which was in practice replaced long ago by wget and curl as standards.

Using Aliases

Adding aliases

Use the alias command to define an alias using a key=value format. The following adds an alias that will add default parameters to the ls command:

alias my-script='ls -F'

Reminder

Aliases must be defined in each shell session. Aliases are conventionally defined in a file $HOME/.aliases and sourced from there during the shell initialization. If you update your .aliases file, be sure to source it in order to use that version of the alias.

Deleting Aliases

Use the unalias command to delete an alias in the current shell. To remove that ls alias, do this:

unalias ls

Details

Quick Overview of Shell Interpolation Process

Here's a quick overview of how the shell handles aliases. This generally applies to inline variables and subshells, but we're specifically addressing aliases here.

  1. Shell parses the command string into an array of tokens, separated by spaces,
  2. The command parameter is checked to see if there's an alias by that name, and
  3. If the global-parameter param -g was present in the creation of the alias, it will check each token in the command array to see if it matches a defined aliases,
  4. Finally, the alias name is replaced with the alias value and the processing continues.
graph TD
    A[Parse command into tokens] --> B[Check if first token is alias]
    B --> C{Was alias created with -g?}
    C -- Yes --> D[Check all tokens for alias matches]
    C -- No --> E[Skip global alias check]
    D --> F[Replace matched aliases with values]
    E --> F
    F --> G[Continue processing command]

Less Quick Interpolation-process Overview

Without using aliases, we usually use the ls command something like this;

ls -ld /usr/local/lib/*

When using our ls alias, defined as ls -ld, we would issue this command, since the alias provides the -ld params:

ls /usr/local/lib/*

What happens next in a little more detail is this:

  1. The shell parses it out into this array:
[ `ls', '/usr/local/lib/*']
  1. The shell checks the command param for a matching alias and performs the replacement. In this case, updating the command array to include the -ld param:
[ `ls', '-ld', '/usr/local/lib/*']
  1. If the global alias option is set (i.e., alias-g ...), every element of the command array is checked, and if any match an alias, that entry in the array will be replaced with the alias text.

Chaining Aliases/Trailing Space

By default, aliases only substitute the command parameter, but there are a few ways to have alias replace parameters too.

If you append a space to your alias, it will also cause the next parameter to be treated as an alias, if it exists. This can be useful for using command aliases with aliases containing parameters. You can chain these aliases together.

Here's a simple git-related example, where we define an alias g to be 'git', including a trailing space, and alias for the git pull and status commands.

alias g='git '
alias s='status '
alias p='pull '
Which allows us to chain the aliases, like this, expanding both 'g', the command param, as well as the subsequent parameter:

# This will run "git pull"
$ g p

# This will run "git status"
$ g s

Disabling aliases

Reminder

TL;DR: You can disable individual aliases by escaping any character in the token string or by single-quoting it.

If any character in the string is escaped, the interpolation will be suppressed. This is often seen with command-plus-param aliases (like the ll example), when you don't want those parameters, in which case, you'd run \ll. Technically, you can also run l\l, but use the prefix format for clarity.

That also means that you have to make sure that there are no escaped characters in the string, such as a string with screen-control characters.

Shell Script Expansion

By default, the Bash Manual

You can enable alias expansion by setting expand_aliases like this:

shopt -s expand_aliases

Shell-specific Functionality

Zsh Alias Anomalies

Zsh provides a -g parameter, which, when passed to the alias command, will expand the alias in any position in the command. By default, it only checks the command parameter, which is usually the first one, but when you pass -g, it results in all parameters in the command's being checked for defined aliases.

More info

*

Examples

  • Create a simple alias for listing files:

    alias ll='ls -alF'
    

  • Create an alias with multiple commands:

    alias backup='tar -czvf backup.tar.gz ~/important_files && echo "Backup complete"'
    

  • Remove a previously created alias:

    unalias ll
    

  • List all current aliases:

    alias
    

  • List a single aliases:

    alias ll
    

See Also

  1. Bash Aliases
  2. Aliases in Bash
  3. Zsh Shell Grammar: aliasing
  4. rothgar/mastering-zsh/.../docs/helpers/aliases
  5. What could be preventing my alias from expanding in a shell script

Alternatives

  • Shell Functions: For more complex operations with arguments:

    ll() {
      ls -alF "$@"
    }
    

  • Shell Scripts: For very complex operations:

    #!/bin/bash
    # myscript.sh
    ls -alF "$@"
    

  • Command Substitution: For dynamic commands:

    echo "The current directory is $(pwd)"
    The current directory is /home/eberg