Alias (Unix shell)
Encyclopedia
In computing
Computing
Computing is usually defined as the activity of using and improving computer hardware and software. It is the computer-specific part of information technology...

, alias is a command
Command (computing)
In computing, a command is a directive to a computer program acting as an interpreter of some kind, in order to perform a specific task. Most commonly a command is a directive to some kind of command line interface, such as a shell....

 in various command line interpreters (shells
Shell (computing)
A shell is a piece of software that provides an interface for users of an operating system which provides access to the services of a kernel. However, the term is also applied very loosely to applications and may include any software that is "built around" a particular component, such as web...

) such as Unix shell
Unix shell
A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems...

s, 4DOS
4DOS
4DOS is a command line interpreter by JP Software, designed to replace the default command interpreter COMMAND.COM in DOS and Windows 95/98/Me. The 4DOS family of programs are meant to replace the default command processor. 4OS2 and 4NT replace CMD.EXE in OS/2 and Windows NT respectively...

/4NT
4NT
Take Command Console , formerly known as 4DOS for Windows NT and 4NT, is a command line interpreter by JP Software, designed as a substitute for the default command interpreter in Microsoft Windows...

 and Windows PowerShell
Windows PowerShell
Windows PowerShell is Microsoft's task automation framework, consisting of a command-line shell and associated scripting language built on top of, and integrated with the .NET Framework...

, 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
MS-DOS
MS-DOS is an operating system for x86-based personal computers. It was the most commonly used member of the DOS family of operating systems, and was the main operating system for IBM PC compatible personal computers during the 1980s to the mid 1990s, until it was gradually superseded by operating...

 and Microsoft Windows
Microsoft Windows
Microsoft Windows is a series of operating systems produced by Microsoft.Microsoft introduced an operating environment named Windows on November 20, 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces . Microsoft Windows came to dominate the world's personal...

 operating systems is provided by the DOSKey
DOSKey
DOSKey is a utility for MS-DOS and Microsoft Windows that adds command history, macro functionality, and improved editing features to the command line interpreters COMMAND.COM and cmd.exe...

 command-line utlility.

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 source
Source (command)
source is a Unix command that evaluates a file or resource as a Tcl script. When a return statement is reached, the script ends. Some bash scripts should be run using the source your-script syntax, e.g., if they contain a change directory command and the user intends that he be left in that...

d from a separate file, typically named .alias (or .alias-bash, .alias-csh, etc., if multiple shells may be used).

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
C shell
The C shell is a Unix shell that was created by Bill Joy while a graduate student at University of California, Berkeley in the late 1970s. It has been distributed widely, beginning with the 2BSD release of the BSD Unix system that Joy began distributing in 1978...

 or tcsh
Tcsh
tcsh is a Unix shell based on and compatible with the C shell . It is essentially the C shell with programmable command line completion, command-line editing, and a few other features.-History:...

 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.

History

In Unix, aliases were introduced in the C shell
C shell
The C shell is a Unix shell that was created by Bill Joy while a graduate student at University of California, Berkeley in the late 1970s. It has been distributed widely, beginning with the 2BSD release of the BSD Unix system that Joy began distributing in 1978...

 and thus survive in descendent shells such as tcsh
Tcsh
tcsh is a Unix shell based on and compatible with the C shell . It is essentially the C shell with programmable command line completion, command-line editing, and a few other features.-History:...

 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
Bourne shell
The Bourne shell, or sh, was the default Unix shell of Unix Version 7 and most Unix-like systems continue to have /bin/sh - which will be the Bourne shell, or a symbolic link or hard link to a compatible shell - even when more modern shells are used by most users.Developed by Stephen Bourne at AT&T...

, which had the more powerful facility of functions. The alias concept was imported into Bourne Again Shell (bash) and the Korn shell
Korn shell
The Korn shell is a Unix shell which was developed by David Korn in the early 1980s and announced at USENIX on July 14, 1983. Other early contributors were AT&T Bell Labs developers Mike Veach, who wrote the emacs code, and Pat Sullivan, who wrote the vi code...

 (ksh). Shells such as these, that support both functions and aliases, recommend using functions where possible. Cases where aliases are necessary include the use of chained aliases (bash and ksh).

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

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:

*dir

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.

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

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:

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.

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:

perl -pe 's/^(.*) foo/$1 bar/;'


You cannot do

alias foo2bar='perl -pe \'s/^(.*) foo/$1 bar/;\ # WRONG: backslashes do not escape the next character inside single quotes


But you can use single quotes quoted inside double quotes

alias foo2bar='perl -pe '"'"'s/^(.*) foo/$1 bar/;'"'"'


See this explanation.

You may also consider using a function instead of an alias.

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
Rm (Unix)
rm is a basic UNIX command used to remove objects such as files, directories, device nodes, symbolic links, and so on from the filesystem...

 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

Alternatives

Aliases should usually be kept simple. Where it would not be simple, the recommendation is usually to use one of the following:
  • Shell script
    Shell script
    A shell script is a script written for the shell, or command line interpreter, of an operating system. It is often considered a simple domain-specific programming language...

    s, which essentially provide the full ability to create new system commands.

  • Symbolic link
    Symbolic link
    In computing, a symbolic link is a special type of file that contains a reference to another file or directory in the form of an absolute or relative path and that affects pathname resolution. Symbolic links were already present by 1978 in mini-computer operating systems from DEC and Data...

    s, 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 "$@" ; }

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK