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 by another string. It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command. Aliasing functionality in the MS-DOS and Microsoft Windows operating systems is provided by the DOSKey command-line utility.
An alias will last for the life of the shell session. Regularly used aliases can be set from 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 upon the start of the corresponding shell session. The alias commands may either be written in the config file directly or sourced from a separate file, typically named .alias (or .alias-bash, .alias-csh, etc., if multiple shells may be used).
Video Alias (command)
Creating aliases
Unix
Non-persistent aliases can be created by supplying name/value pairs as arguments for the alias command. In Unix shells the syntax is:
C shell
The corresponding syntax in the C shell or tcsh shell is:
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.
4DOS
In the 4DOS/4NT shell the following syntax is used to define cp
as an alias for the 4DOS copy
command:
alias cp copy
Windows PowerShell
To create a new alias in Windows PowerShell, the new-alias
cmdlet can be used:
This creates a new alias called ci
that will be replaced with the copy-item
cmdlet when executed.
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.
Maps Alias (command)
History
In Unix, aliases were introduced in the C shell and 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).
Viewing currently defined aliases
To view defined aliases the following commands can be used:
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:
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:
*dir
Changing aliases
In Windows PowerShell, the set
verb can be used with the alias
cmdlet to change an existing alias:
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.
Removing aliases
In Unix shells and 4DOS/4NT, aliases can be removed by executing the unalias
command:
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
:
Features
Chaining
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:
allows:
for a long listing, where "long" is also evaluated as an alias.
Quoting quotes
To define an alias with single quotes, which itself needs to contain single quotes, you need to use several concatenated quoted strings. For example, to define an alias which would do:
You cannot do
However, you can surround \'
with single quotes to produce '\''
, as in the following:
Also, you can use single quotes quoted inside double quotes:
.
You may also consider using a function instead of an alias.
Command arguments
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.
The Bash and Korn shells instead use shell functions -- see Alternatives below.
Typical aliases
Some commonly used, but deprecated, aliases in the Bash shell:
Standard aliases of Windows PowerShell include:
Alternatives
Aliases should usually be kept simple. Where it would not be simple, the recommendation is usually to use 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 user's $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:
To make ls itself a function (note that "command ls" is Bash-specific, and that older Bourne shells would have used "/bin/ls" instead):
References
External links
alias
: 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)
- Find out if a command is aliased to another command
Source of the article : Wikipedia