Alias (command)
Since aliases are defined only for a shell session, regularly used aliases are often defined in a session startup shell script such as 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. FeaturesDefineThe following is an example that defines 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 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 In PowerShell, the set-alias ci cls
In 4DOS/4NT shell, the eset /a cp
ListTo 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
RemoveIn Unix shells and 4DOS/4NT, aliases can be removed via 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 remove-item alias:ci
IgnoreIn Unix shells, an aliased word can be used without replacement by using quotes. For example, consider the following command that defines an alias alias ls='ls -la'
In 4DOS/4NT shell, an asterisk is used. For example, the following defines alias dir = *dir /2/p
ChainingTypically, aliases are used to replace the first word of a command line, but some shells such as For example, the following defines alias list='ls '
alias long='-Flas'
Then, command line The behavior provided by chaining is not possible via shell functions. Command argumentsIn the C Shell, arguments can be embedded inside the command using the string alias ls-more 'ls \!* | more'
alias ls-more 'ls | more'
would instead expand to Some shells such as bash and ksh do not support this syntax, but do provide for similar functionality via shell functions — see § Alternatives below. AlternativesBest practice is to only define an alias for a relatively simple command. Alternatives for more complicated logic include:
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 References
Further reading
External linksThe Wikibook Guide to Unix has a page on the topic of: Commands
|