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

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:
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:
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.
- Shell parses the command string into an array of tokens, separated by spaces,
- The command parameter is checked to see if there's an alias by that name, and
- 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, - 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;
When using our ls
alias, defined as ls -ld
, we would issue this command, since the alias provides the -ld
params:
What happens next in a little more detail is this:
- The shell parses it out into this array:
- 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:
- 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.
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:
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:
-
Create an alias with multiple commands:
-
Remove a previously created alias:
-
List all current aliases:
-
List a single aliases:
See Also¶
- Bash Aliases
- Aliases in Bash
- Zsh Shell Grammar: aliasing
- rothgar/mastering-zsh/.../docs/helpers/aliases
- What could be preventing my alias from expanding in a shell script
Alternatives¶
-
Shell Functions: For more complex operations with arguments:
-
Shell Scripts: For very complex operations:
-
Command Substitution: For dynamic commands: