Alias (command)

1

In computing, alias is a command in various command-line interpreters (shells), 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. is available in Unix shells, AmigaDOS, 4DOS/4NT, FreeDOS, KolibriOS, Windows PowerShell, ReactOS, and the EFI shell. 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 rc file (such as ) 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.

History

In 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.

Usage

Creating aliases

Common Unix shells

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 is read in the shell, it will be replaced with and that command will be executed instead.

4DOS

In the 4DOS/4NT shell the following syntax is used to define as an alias for the 4DOS command: alias cp copy

Windows PowerShell

To create a new alias in Windows PowerShell, the cmdlet can be used: This creates a new alias called that will be replaced with the 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.

Viewing currently defined aliases

To view defined aliases the following commands can be used:

Overriding aliases

In 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: To override this alias and execute the 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 causes the unaliased to be invoked, preventing recursive alias expansion. Also the user can get the unaliased behaviour of at the command line by using the same syntax: *dir

Changing aliases

In Windows PowerShell, the verb can be used with the cmdlet to change an existing alias: The alias will now point to the command. In the 4DOS/4NT shell, the command provides an interactive command line to edit an existing alias: eset /a cp The causes the alias 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 command: In Windows PowerShell, the alias can be removed from the alias:\ drive using :

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.

Command arguments

In the C Shell, arguments can be embedded inside the command using the string !. For example, with this alias: expands to to list the contents of the directories /etc and /usr, pausing after every screenful. Without !, would instead expand to which incorrectly attempts to open the directories in more. The Bash and Korn shells instead use shell functions — see § Alternatives below.

Alternatives

Aliases 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: To prevent a function from calling itself recursively, use : In older Bourne shells use instead of.

This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.

Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc.
Bliptext is not affiliated with or endorsed by Wikipedia or the Wikimedia Foundation.

View original