alias (command)
From Wikipedia, the free encyclopedia
In computing, alias is a command in various command line interpreters (shells) such as Unix shells, 4DOS/4NT and Windows PowerShell, which enables a replacement of a word with another string. It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command. Typically, an alias will last for the life of the shell session but regularly used aliases can be placed in the shell's configuration file (~/.cshrc or the systemwide /etc/csh.cshrc for csh, or ~/.bashrc or the systemwide /etc/bashrc or /etc/bash.bashrc for bash) so that they will be available for all shell sessions.
Contents |
[edit] Creating aliases
Aliases can be created by supplying name/value pairs as arguments for the alias command. An example of the Bash shell syntax is:
alias copy="cp"
The corresponding syntax in the C shell or tcsh shell is:
alias copy "cp"
This alias means that when the command copy is read in the shell, it will be replaced with cp and that command will be executed instead.
In the 4DOS/4NT shell the following syntax is used to define cp as an alias for the 4DOS copy command:
alias cp copy
To create a new alias in Windows PowerShell, the new verb can be used with the alias cmdlet:
new-alias ci copy-item
This creates a new alias called ci that will be replaced with the copy-item cmdlet when executed.
[edit] History
In Unix, aliases were introduced in the C shell and thus survive in descendent shells such as tcsh and bash. C shell aliases were strictly limited to one line in a shell language where all complex constructs required more, but still useful for creating simple shortcut commands. Aliases were absent from the Bourne shell, which had the more powerful facility of functions. The alias concept was imported into bash, an sh/csh hybrid, but is essentially only useful when the new command being defined has the same name as one of its subcommands (a deprecated practice), or when the nearly unknown facility for chained aliases is needed. In all other cases, bash functions are the recommended practice.
[edit] Viewing currently defined aliases
To view defined aliases the following commands can be used:
alias # Used without arguments; displays a list of all current aliases alias -p # Analogous to the above; not available in 4DOS/4NT and PowerShell alias myAlias # Displays the command for a defined alias
[edit] Overriding aliases
In Unix shells, if an alias exists for a command, it is possible to override the alias by surrounding the command with quotes or prefixing it with a backslash. For example, consider the following alias definition:
alias ls='ls -la'
To override this alias and execute the ls command as it was originally defined, the following syntax can be used:
'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 causes the unaliased dir to be invoked, preventing recursive alias expansion. Also the user can get the unaliased behaviour of dir at the command line by using the same syntax:
*ls
[edit] Changing aliases
In Windows PowerShell, the set verb can be used with the alias cmdlet to change an existing alias:
set-alias ci cls
The alias ci will now point to the cls command.
In the 4DOS/4NT shell, the eset command provides an interactive command line to edit an existing alias:
eset /a cp
The /a causes the alias cp to be edited, as opposed to an environment variable of the same name.
[edit] Removing aliases
In Unix shells and 4DOS/4NT, aliases can be removed by executing the unalias command:
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:
remove-item alias:ci # Removes the ci alias
[edit] Chained aliases in Bash
In Bash, a setup of:
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 checked for being an alias. This is a distinctive but almost unused feature of Bash aliases which cannot be concisely emulated in a function.
[edit] Typical aliases
Some commonly used, but deprecated, aliases in the Bash shell:
alias ls='ls --color=auto' # use colors alias la='ls -Fa' # list all files alias ll='ls -Fls' # long listing format alias rm='rm -i' # prompt before overwrite (but dangerous, see Rm for a better approach) alias cp='cp -i' # prompt before overwrite (same general problem as the rm) alias mv='mv -i' # prompt before overwrite (same general problem as the rm) alias vi='vim' # use improved vi editor
Standard aliases of Windows PowerShell include:
new-alias cd set-location new-alias ls get-childitem new-alias dir get-childitem new-alias echo write-output new-alias ps get-process new-alias kill stop-process
[edit] Alternatives
Generally, in Bash, aliases should be replaced with one of the following:
- Shell Scripts, which essentially provide the full ability to create new system commands.
- Symbolic links, either in /usr/local/bin if for all users, or in a users $HOME/bin directory if for personal use. This method is useful for providing an additional way of calling the command, and 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 functions, especially if the command being created needs to modify the internal runtime environment of the shell itself (such as environment variables), needs to change the shell's current working directory, or must be implemented in a way which guarantees they 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 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 make ls itself a function (note that "command ls" is Bash-specific, and that older Bourne shells would have used "/bin/ls" instead):
ls () { command ls --color=auto "$@" ; }
[edit] External links
- : define or display aliases – Commands & Utilities Reference, The Single UNIX® Specification, Issue 7 from The Open Group
- Bash man page for alias
- The alias Command by The Linux Information Project (LINFO)
|
||||||||||||||||||||||||||