alias (command)
This article needs additional citations for verification. (July 2013) |
![]() Example of alias command | |
Original author(s) | Bill Joy |
---|---|
Developer(s) | Various open-source and commercial developers |
Operating system | Unix, Unix-like, AmigaDOS, FreeDOS, Microsoft Windows, ReactOS, AROS, KolibriOS, IBM i |
Platform | Cross-platform |
Type | Command |
alias
is shell command that defines a word that will act like a command.[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 in Unix shells, AmigaDOS, 4DOS/4NT, FreeDOS, KolibriOS, PowerShell, ReactOS, EFI shell,[2] and IBM i.[3] Aliasing functionality in MS-DOS and Command Prompt is provided by the DOSKEY
command.
Since aliases are defined only for a shell session, regularly-used aliases are often defined in a session startup shell script such as .bashrc
. The alias
commands may either be written in the config script directly or sourced from a separate file.
Aliases were introduced in the C shell to survive in descendant shells such as tcsh and bash. 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 the Bourne shell did not offer aliases, but did provide functions, which are more powerful than the csh alias. Eventually, the csh alias was implemented in the bash and ksh shells. With shells that support both functions and aliases but no parameterized inline shell scripts, the use of functions wherever possible is recommended. None-the-less, aliases are necessary where chained aliases are required.
Examples
[edit]Define
[edit]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-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, the set
verb is used to change an existing alias. The following changes the alias ci
to invoke the cls
command.
set-alias ci cls
In 4DOS/4NT shell, the eset
command provides an interactive command line to edit an existing alias. For exmaple:
eset /a cp
List
[edit]To view defined aliases:
alias
To list aliases in a way that allows for re-creating them by sourcing the output (not available in 4DOS/4NT or PowerShell):
alias -p
To report the definition of a particular alias name:
alias myAlias
Ignore
[edit]In Unix shells, it is possible to ignore an alias such that the original command is invoked. 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/p
Remove
[edit]In Unix shells and 4DOS/4NT, aliases can be removed via unalias
. To remove the copy
alias:
unalias copy
To remove all aliases (not available in 4DOS/4NT):
unalias -a
To remove all aliases in 4DOS/4NT:
unalias *
In PowerShell, an alias is removed from the alias:\
drive via remove-item
:
remove-item alias:ci
Features
[edit]Chaining
[edit]An alias usually replaces just the first word. But some shells, such as bash
and ksh
, allow a sequence or words to be replaced. This particular feature is unavailable through the function mechanism.
The usual syntax is to define the first alias with a trailing space character. For instance, using the two aliases:
alias list='ls ' # note the trailing space to trigger chaining alias long='-Flas' # options to ls for a long listing
allows:
list long myfile # becomes "ls -Flas myfile" when run
for a long listing, where "long" is also evaluated as an alias.
Command arguments
[edit]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]
The Bash and Korn shells instead use shell functions — see § Alternatives below.
Alternatives
[edit]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)
The most common form of an alias which includes a few arguments and supports subsequent arguments, can be converted to a shell function following the pattern demonstrated as:
alias ll='ls -Flas' # long listing, alias ll () { ls -Flas "$@" ; } # long listing, function
To prevent a function from calling itself recursively, use command
:
ls () { command ls --color=auto "$@" ; }
In older Bourne shells, use /bin/ls
instead of command ls
.
References
[edit]- ^ 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.
- ^ "EFI Shells and Scripting". Intel. Retrieved 2013-09-25.
- ^ IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). IBM. Retrieved 2020-09-05.
- ^ "Examples of passing arguments given to a command alias". UNIXhelp. University of Edinburgh. Archived from the original on 2012-11-25.
Further reading
[edit]- McElhearn, Kirk (2006). The Mac OS X Command Line: Unix Under the Hood. John Wiley & Sons. ISBN 978-0470113851.
External links
[edit]- The Single UNIX Specification, Version 4 from The Open Group : define or display aliases – Shell and Utilities Reference,
- Bash man page for alias
- The alias Command by The Linux Information Project (LINFO)