Alias (command)
alias is a shell command that defines a word that the shell replaces with associated text before interpreting a command line. 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, and IBM i. 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 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. Nonetheless, aliases are necessary where chained aliases are required.
Features
Define
The following is an example that definesgc 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, 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 example:eset /a cp
List
To view defined aliases:alias
To list aliases in a way that allows for re-creating them by sourcing the output :
alias -p
To report the definition of a particular alias name:
alias myAlias
Remove
In Unix shells and 4DOS/4NT, aliases can be removed viaunalias. To remove the alias:unalias copy
To remove all aliases :
unalias -a
To remove all aliases in 4DOS/4NT:
unalias *
In PowerShell, an alias is removed from the drive via
remove-item:remove-item alias:ci
Ignore
In Unix shells, an aliased word can be used without replacement by using quotes. For example, consider the following command that defines an alias that invokes the original with options. To invoke the original command, the following syntax is used: or.alias ls='ls -la'
In 4DOS/4NT shell, an asterisk is used. For example, the following defines to invoke the original with options. To later invoke the original, the syntax is.
alias dir = *dir /2/p
Chaining
Typically, aliases are used to replace the first word of a command line, but some shells such as and also support chaining replacing subsequent words.For example, the following defines to invoke and to as a set of options. The command alias must end with a space to enable chaining.
alias list='ls '
alias long='-Flas'
Then, command line expands to.
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: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, would instead expand to
ls | more /etc /usr which incorrectly attempts to open the directories in more.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:- A shell script, which provides a rich ability to implement a command
- A symbolic link in the user's
PATH, which 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 - A shell function, especially if the command being created needs to modify the internal runtime environment of the shell 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
alias ll='ls -Flas' can be implemented as function. To prevent a function from calling itself, use command: ls . In older Bourne shells, use /bin/ls instead of command ls.