This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Alias" command – news ·newspapers ·books ·scholar ·JSTOR(July 2013) (Learn how and when to remove this message) |
| alias | |
|---|---|
Example of alias command | |
| Original author | Bill Joy |
| Developers | Variousopen-source andcommercial developers |
| Operating system | Unix,Unix-like,AmigaDOS,FreeDOS,Microsoft Windows,ReactOS,AROS,KolibriOS,IBM i |
| Platform | Cross-platform |
| Type | Command |
alias is ashellcommand that defines a word that the shell replaces with associated text before interpreting a command line.[1] It is often used to enhance productivity by abbreviating a command or for including commonly used arguments with a command. The command is available inUnix shells,AmigaDOS,4DOS/4NT,FreeDOS,KolibriOS,PowerShell,ReactOS,EFI shell,[2] andIBM i.[3] Aliasing functionality inMS-DOS andCommand Prompt is provided by theDOSKEY command.
Since aliases are defined only for a shell session, regularly used aliases are often defined in a session startupshell script such as.bashrc. Thealias commands may either be written in the config script directly orsourced from a separate file.
Aliases were introduced in theC shell to survive in descendant shells such astcsh andbash. As these aliases were limited to one line they were useful for creating relatively simple shortcut commands, but not more complex constructs. Older versions of theBourne shell did not offer aliases, but did provide functions, which are more powerful than the csh alias. Eventually, the csh alias was implemented in thebash andksh shells. With shells that support both functions and aliases but no parameterized inline shell scripts, the use of functions wherever possible is recommended. Nonetheless, aliases are necessary where chained aliases are required.
The following is an example that definesgc to be a command the performs the actiongit commit.
aliasgc='git commit'
In C shell and tcsh there is no equals sign:
aliasgc"git commit"
To define an alias in PowerShell, thenew-alias cmdlet is used:
new-aliascicopy-item
In PowerShell, an alias cannot be used to specify default arguments for a command. Instead, this must be done by adding items to the collection$PSDefaultParameterValues, one of the PowerShell preference variables.
In PowerShell, theset verb is used to change an existing alias. The following changes the aliasci to invoke thecls command.
set-aliascicls
In 4DOS/4NT shell, theeset command provides an interactive command line to edit an existing alias. For example:
eset /a cpTo view defined aliases:
aliasTo list aliases in a way that allows for re-creating them by sourcing the output (not available in 4DOS/4NT or PowerShell):
alias-pTo report the definition of a particular alias name:
aliasmyAliasIn Unix shells and 4DOS/4NT, aliases can be removed viaunalias. To remove thecopy alias:
unaliascopyTo remove all aliases (not available in 4DOS/4NT):
unalias-aTo remove all aliases in 4DOS/4NT:
unalias*In PowerShell, an alias is removed from thealias:\ drive viaremove-item:
remove-itemalias:ci
In Unix shells, an aliased word can be used without replacement by using quotes. For example, consider the following command that defines an aliasls that invokes the originalls with options-la. To invoke the originalls command (without the options), the following syntax is used:'ls' or\ls.
aliasls='ls -la'
In 4DOS/4NT shell, an asterisk is used. For example, the following definesdir to invoke the originaldir (requires asterisk in the definition) with options/2/p. To later invoke the originaldir, the syntax is*dir.
alias dir = *dir /2/pTypically, aliases are used to replace the first word of a command line, but some shells such asbash andksh also support chaining – replacing subsequent words.
For example, the following defineslist to invokels andlong to as a set ofls options. The command alias must end with a space to enable chaining.
aliaslist='ls 'aliaslong='-Flas'
Then, command linelist long myfile expands tols -Flas myfile.
The behavior provided by chaining is not possible via shell functions.
In theC Shell,arguments can be embedded inside the command using the string\!*. For example, with this alias:
aliasls-more'ls \!* | more'
ls-more /etc /usr expands tols /etc /usr | more to list the contents of the directories /etc and /usr, pausing after every screenful. Without\!*,
aliasls-more'ls | more'
would instead expand tols | more /etc /usr which incorrectly attempts to open the directories inmore.[4]
Some shells such as bash and ksh do not support this syntax, but do provide for similar functionality via shell functions — see§ Alternatives below.
Best practice is to only define an alias for a relatively simple command. Alternatives for more complicated logic include:
PATH (such as/bin); in some cases may allow access to a buried command function for the small number of commands that use their invocation name to select the mode of operationrm,cp,mv and so forth)A relatively simple alias that includes a few arguments and supports subsequent arguments, can be converted to a shell function in a relatively straightforward process. For example, aliasalias ll='ls -Flas' can be implemented as functionll () { ls -Flas "$@" ;}. To prevent a function fromcalling itself, usecommand:ls () { command ls --color=auto "$@" ; }. In older Bourne shells, use/bin/ls instead ofcommand ls.
alias: define or display aliases – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open Group