Alias (command)

Last updated
alias
Original author(s) Bill Joy
Developer(s) Various open-source and commercial developers
Operating system Unix, Unix-like, AmigaDOS, Microsoft Windows, ReactOS, AROS, KolibriOS, IBM i
Platform Cross-platform
Type 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. alias is available in Unix shells, AmigaDOS, 4DOS/4NT, KolibriOS, Windows PowerShell, ReactOS, and the EFI shell. [2] Aliasing functionality in the MS-DOS and Microsoft Windows operating systems is provided by the DOSKey command-line utility.

Contents

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 .bashrc) 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 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). The alias command has also been ported to the IBM i operating system. [3]

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:

aliasgc='git commit'

C shell

The corresponding syntax in the C shell or tcsh shell is:

alias gc "git commit"

This alias means that when the command gc is read in the shell, it will be replaced with git commit 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:

new-aliascicopy-item

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.

Viewing currently defined aliases

To view defined aliases the following commands can be used:

alias# Used without arguments; displays a list of all current aliasesalias-p# List aliases in a way that allows re-creation by sourcing the output; not available in 4DOS/4NT and PowerShellaliasmyAlias# Displays the command for a defined alias

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:

aliasls='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-aliascicls

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:

unaliascopy# Removes the copy aliasunalias-a# The -a switch will remove all aliases; not available in 4DOS/4NTunalias*# 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-itemalias:ci# Removes the ci alias

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:

aliaslist='ls '# note the trailing space to trigger chainingaliaslong='-Flas'# options to ls for a long listing

allows:

listlongmyfile# becomes "ls -Flas myfile" when run

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:

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. [4]

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:

aliasll='ls -Flas'# long listing, alias ll(){ls-Flas"$@";}# long listing, function

To prevent a function from calling itself recursively, use command :

ls(){commandls--color=auto"$@";}

In older Bourne shells use /bin/ls instead of command ls.

Related Research Articles

<span class="mw-page-title-main">KornShell</span> Bourne shell backward compatible Unix shell created by David Korn

KornShell (ksh) is a Unix shell which was developed by David Korn at Bell Labs in the early 1980s and announced at USENIX on July 14, 1983. The initial development was based on Bourne shell source code. Other early contributors were Bell Labs developers Mike Veach and Pat Sullivan, who wrote the Emacs and vi-style line editing modes' code, respectively. KornShell is backward-compatible with the Bourne shell and includes many features of the C shell, inspired by the requests of Bell Labs users.

<span class="mw-page-title-main">Shell script</span> Script written for the shell, or command line interpreter, of an operating system

A shell script is a computer program designed to be run by a Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manipulation, program execution, and printing text. A script which sets up the environment, runs the program, and does any necessary cleanup or logging, is called a wrapper.

cd (command) Computer command in various operating systems

The cd command, also known as chdir, is a command-line shell command used to change the current working directory in various operating systems. It can be used in shell scripts and batch files.

pwd Directory information command on various operating systems

In Unix-like and some other operating systems, the pwd command writes the full pathname of the current working directory to the standard output.

<span class="mw-page-title-main">4DOS</span> Command line interpreter

4DOS is a command-line interpreter by JP Software, designed to replace the default command interpreter COMMAND.COM in Microsoft DOS and Windows. It was written by Rex C. Conn and Tom Rawson and first released in 1989. Compared to the default, it has a large number of enhancements.

An environment variable is a user-definable value that can affect the way running processes will behave on a computer. Environment variables are part of the environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.

In computer programming, glob patterns specify sets of filenames with wildcard characters. For example, the Unix Bash shell command mv *.txttextfiles/ moves all files with names ending in .txt from the current directory to the directory textfiles. Here, * is a wildcard standing for "any string of characters except /" and *.txt is a glob pattern. The other common wildcard is the question mark (?), which stands for one character. For example, mv?.txtshorttextfiles/ will move all files named with a single character followed by .txt from the current directory to directory shorttextfiles, while ??.txt would match all files whose name consists of 2 characters followed by .txt.

<span class="mw-page-title-main">Pipeline (Unix)</span> Mechanism for inter-process communication using message passing

In Unix-like computer operating systems, a pipeline is a mechanism for inter-process communication using message passing. A pipeline is a set of processes chained together by their standard streams, so that the output text of each process (stdout) is passed directly as input (stdin) to the next one. The second process is started as the first process is still executing, and they are executed concurrently. The concept of pipelines was championed by Douglas McIlroy at Unix's ancestral home of Bell Labs, during the development of Unix, shaping its toolbox philosophy. It is named by analogy to a physical pipeline. A key feature of these pipelines is their "hiding of internals". This in turn allows for more clarity and simplicity in the system.

In Unix-like and some other operating systems, find is a command-line utility that locates files based on some user-specified criteria and either prints the pathname of each matched object or, if another action is requested, performs that action on each matched object.

rm (Unix) Unix command utility

rm is a basic command on Unix and Unix-like operating systems used to remove objects such as computer files, directories and symbolic links from file systems and also special files such as device nodes, pipes and sockets, similar to the del command in MS-DOS, OS/2, and Microsoft Windows. The command is also available in the EFI shell.

<span class="mw-page-title-main">Comparison of command shells</span>

A command shell is a command-line interface to interact with and manipulate a computer's operating system.

In computing, tee is a command in command-line interpreters (shells) using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. It is primarily used in conjunction with pipes and filters. The command is named after the T-splitter used in plumbing.

Take Command Console (TCC), formerly known as 4DOS for Windows NT (4NT), is a command-line interpreter by JP Software, designed as a substitute for the default command interpreter in Microsoft Windows, CMD.EXE.

move (command)

In computing, move is a command in various command-line interpreters (shells) such as COMMAND.COM, cmd.exe, 4DOS/4NT, and PowerShell. It is used to move one or more files or directories from one place to another. The original file is deleted, and the new file may have the same or a different name. The command is analogous to the Unix mv command and to the OpenVOS move_file and move_dircommands.

ren (command)

In computing, ren is a command in various command-line interpreters (shells) such as COMMAND.COM, cmd.exe, 4DOS, 4NT and Windows PowerShell. It is used to rename computer files and in some implementations also directories. It is analogous to the Unix mv command. However, unlike mv, ren cannot be used to move files, as a new directory for the destination file may not be used. Alternatively, move may be used if available. On versions of MS-DOS that do not support the move command, the user would simply copy the file to a new destination, and then delete the original file. A notable exception to this rule is DOSBox, in which ren may be used to move a file, since move is not supported.

<span class="mw-page-title-main">TYPE (DOS command)</span>

In computing, type is a command in various command-line interpreters (shells) such as COMMAND.COM, cmd.exe, 4DOS/4NT and Windows PowerShell used to display the contents of specified files on the computer terminal. The analogous Unix command is cat.

del (command) Command in various operating systems

In computing, del is a command in command-line interpreters (shells) such as COMMAND.COM, cmd.exe, 4DOS, NDOS, 4OS2, 4NT and Windows PowerShell. It is used to delete one or more files or directories from a file system.

In computing, pushd and popd are commands used to work with the command line directory stack. They are available on command-line interpreters such as 4DOS, Bash, C shell, tcsh, Hamilton C shell, KornShell, cmd.exe, and PowerShell for operating systems such as Windows and Unix-like systems.

help (command) Command in various command line shells

In computing, help is a command in various command line shells such as COMMAND.COM, cmd.exe, Bash, qshell, 4DOS/4NT, Windows PowerShell, Singularity shell, Python, MATLAB and GNU Octave. It provides online information about available commands and the shell environment.

<span class="mw-page-title-main">Command-line interface</span> Computer interface that uses text

A command-line interface (CLI) is a means of interacting with a device or computer program with commands from a user or client, and responses from the device or program, in the form of lines of text. Such access was first provided by computer terminals starting in the mid-1960s. This provided an interactive environment not available with punched cards or other input methods.

References

  1. Rugheimer, Hannes (2020-06-10). AmigaDOS quick reference : Rügheimer, Hannes : Free Download, Borrow, and Streaming : Internet Archive. ISBN   9781557550491 . Retrieved 2020-09-12 via Internet Archive.
  2. "EFI Shells and Scripting". Intel . Retrieved 2013-09-25.
  3. IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). IBM . Retrieved 2020-09-05.
  4. "Examples of passing arguments given to a command alias". UNIXhelp. University of Edinburgh. Archived from the original on 2012-11-25.

Further reading