Alias (command)
In computing, alias is a command in various command-line interpreters (shells), which enables a replacement of a word by another string.[1] It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command. An alias will last for the life of the shell session. Regularly used aliases can be set from the shell's rc file (such as HistoryIn Unix, aliases were introduced in the C shell to survive in descendant shells such as tcsh and bash. C shell aliases were strictly limited to one line. This was useful for creating simple shortcut commands, but not more complex constructs. Older versions of the Bourne shell did not offer aliases, but it did provide functions, which are more powerful than the csh alias concept. The alias concept from csh was imported into Bourne Again Shell (bash) and the Korn shell (ksh). With shells that support both functions and aliases but no parameterized inline shell scripts, the use of functions wherever possible is recommended. Cases where aliases are necessary include situations where chained aliases are required (bash and ksh). The alias command has also been ported to the IBM i operating system.[3] UsageCreating aliasesCommon Unix shellsNon-persistent aliases can be created by supplying name/value pairs as arguments for the alias command. In Unix shells the syntax is: alias gc='git commit'
C shellThe corresponding syntax in the C shell or tcsh shell is: alias gc "git commit"
This alias means that when the command 4DOSIn the 4DOS/4NT shell the following syntax is used to define alias cp copy Windows PowerShellTo create a new alias in Windows PowerShell, the new-alias ci copy-item
This creates a new alias called 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. Viewing currently defined aliasesTo view defined aliases the following commands can be used: alias # Used without arguments; displays a list of all current aliases
alias -p # List aliases in a way that allows re-creation by sourcing the output; not available in 4DOS/4NT and PowerShell
alias myAlias # Displays the command for a defined alias
Overriding aliasesIn Unix shells, it is possible to override an alias by quoting any character in the alias name when using the alias. For example, consider the following alias definition: alias ls='ls -la'
To override this alias and execute the 'ls' or \ls In the 4DOS/4NT shell it is possible to override an alias by prefixing it with an asterisk. For example, consider the following alias definition: alias dir = *dir /2/p The asterisk in the 2nd instance of *dir Changing aliasesIn Windows PowerShell, the set-alias ci cls
The alias In the 4DOS/4NT shell, the eset /a cp The Removing aliasesIn Unix shells and 4DOS/4NT, aliases can be removed by executing the unalias copy # Removes the copy alias
unalias -a # The -a switch will remove all aliases; not available in 4DOS/4NT
unalias * # 4DOS/4NT equivalent of `unalias -a` - wildcards are supported
In Windows PowerShell, the alias can be removed from the alias:\ drive using remove-item alias:ci # Removes the ci alias
FeaturesChainingAn alias usually replaces just the first word. But some shells, such as 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 argumentsIn the C Shell, arguments can be embedded inside the command using the string \!*. For example, with this alias: alias ls-more 'ls \!* | more'
alias ls-more 'ls | more'
would instead expand to The Bash and Korn shells instead use shell functions — see § Alternatives below. AlternativesAliases should usually be kept simple. Where it would not be simple, the recommendation is usually to use one of the following:
The most common form of aliases, which just add a few options to a command and then include the rest of the command line, can be converted easily to shell functions following this pattern: alias ll='ls -Flas' # long listing, alias
ll () { ls -Flas "$@" ; } # long listing, function
To prevent a function from calling itself recursively, use ls () { command ls --color=auto "$@" ; }
In older Bourne shells use References
Further reading
External linksThe Wikibook Guide to Unix has a page on the topic of: Commands
|