An alias is a shell command that substitutes a word with associated text before execution, enhancing productivity by abbreviating commands or including common arguments. Aliases are supported in many environments such as Unix shells, PowerShell, FreeDOS, and EFI shell. In MS-DOS and Command Prompt, aliasing is provided via the DOSKEY command. Since aliases last only per session, they're often defined in startup shell scripts like .bashrc, either directly or by being sourced from files. Originally introduced with the C shell, aliases are limited to one line and coexist with functions in shells like bash and ksh, where functions are preferred for complexity but aliases remain useful for chaining commands.
Features
Define
The following is an example that defines gc to be a command the performs the action git commit.
alias gc='git commit'In C shell and tcsh there is no equals sign:
alias gc "git commit"To define an alias in PowerShell, the new-alias cmdlet is used:
new-alias ci copy-itemIn 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, the set verb is used to change an existing alias. The following changes the alias ci to invoke the cls command.
set-alias ci clsIn 4DOS/4NT shell, the eset command provides an interactive command line to edit an existing alias. For example:
eset /a cpList
To 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:
alias myAliasRemove
In Unix shells and 4DOS/4NT, aliases can be removed via unalias. To remove the copy alias:
unalias copyTo remove all aliases (not available in 4DOS/4NT):
unalias -aTo remove all aliases in 4DOS/4NT:
unalias *In PowerShell, an alias is removed from the alias:\ drive via remove-item:
remove-item alias:ciIgnore
In Unix shells, an aliased word can be used without replacement by using quotes. For example, consider the following command that defines an alias ls that invokes the original ls with options -la. To invoke the original ls command (without the options), the following syntax is used: 'ls' or \ls.
alias ls='ls -la'In 4DOS/4NT shell, an asterisk is used. For example, the following defines dir to invoke the original dir (requires asterisk in the definition) with options /2/p. To later invoke the original dir, the syntax is *dir.
alias dir = *dir /2/pChaining
Typically, aliases are used to replace the first word of a command line, but some shells such as bash and ksh also support chaining – replacing subsequent words.
For example, the following defines list to invoke ls and long to as a set of ls options. The command alias must end with a space to enable chaining.
alias list='ls ' alias long='-Flas'Then, command line list long myfile expands to ls -Flas myfile.
The behavior provided by chaining is not possible via shell functions.
Command arguments
In the C Shell, arguments can be embedded inside the command using the string \!*. For example, with this alias:
alias ls-more 'ls \!* | more'ls-more /etc /usr expands to ls /etc /usr | more to list the contents of the directories /etc and /usr, pausing after every screenful. Without \!*,
alias ls-more 'ls | more'would instead expand to ls | more /etc /usr which incorrectly attempts to open the directories in more.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.
Alternatives
Best practice is to only define an alias for a relatively simple command. Alternatives for more complicated logic include:
- Shell script, provides a rich ability to implement a command
- Symbolic link in the user's 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 operation
- Shell function, especially if the command being created needs to modify the internal runtime environment of the shell (such as environment variables), needs to change the shell's working directory, or must be implemented in a way which guarantees that it appear in the command search path for anything but an interactive shell (especially any "safer" version of rm, 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, alias alias ll='ls -Flas' can be implemented as function ll () { ls -Flas "$@" ;}. To prevent a function from calling itself, use command: ls () { command ls --color=auto "$@" ; }. In older Bourne shells, use /bin/ls instead of command ls.
Further reading
- McElhearn, Kirk (2006). The Mac OS X Command Line: Unix Under the Hood. John Wiley & Sons. ISBN 978-0470113851.
External links
The Wikibook Guide to Unix has a page on the topic of: Commands- alias: define or display aliases – Shell and Utilities Reference, The Single UNIX Specification, Version 5 from The Open Group
- Bash man page for alias
- The alias Command by The Linux Information Project (LINFO)
References
Rugheimer, Hannes (2020-06-10). AmigaDOS quick reference : Rügheimer, Hannes : Free Download, Borrow, and Streaming : Internet Archive. ISBN 9781557550491. Retrieved 2020-09-12 – via Internet Archive. 9781557550491 ↩
"EFI Shells and Scripting". Intel. Retrieved 2013-09-25. http://software.intel.com/en-us/articles/efi-shells-and-scripting/ ↩
IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). IBM. Retrieved 2020-09-05. /wiki/IBM ↩
"Examples of passing arguments given to a command alias". UNIXhelp. University of Edinburgh. Archived from the original on 2012-11-25. https://web.archive.org/web/20121125074502/http://unixhelp.ed.ac.uk/shell/alias_csh2.1.html ↩