alias (command)

In computing, alias is a command in various command-line interpreters (shells) such as Unix shells, AmigaDOS, 4DOS/4NT, KolibriOS[1] and Windows PowerShell, which enables a replacement of a word by another string.[2] It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command. The command is also available in the EFI shell.[3] Aliasing functionality in the MS-DOS and Microsoft Windows operating systems is provided by the DOSKey command-line utility. The ReactOS Command Prompt also includes the alias command to set, remove and show aliases.

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

alias
Operating systemUnix and Unix-like, AmigaDOS, Microsoft Windows, ReactOS, AROS, KolibriOS
TypeCommand

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

Usage

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:

 alias copy='cp'

C shell

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.

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-alias ci copy-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 aliases
 alias -p       # List aliases in a way that allows re-creation by sourcing the output; 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

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:

 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 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:

$ 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

However, you can surround \' with single quotes to produce '\'', as in the following:

$ alias foo2bar='perl -pe '\''s/^(.*) foo/$1 bar/;'\''' # Put single quotes around your \' like '\''

Also, you can use single quotes quoted inside double quotes:

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

.[4]

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

The Bash and Korn shells instead use shell functions — see Alternatives below.

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

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:

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

References

  1. ^ http://wiki.kolibrios.org/wiki/Shell
  2. ^ https://archive.org/details/1988-rugheimer-spanik-amigados-quick-reference
  3. ^ "EFI Shells and Scripting". Intel. Retrieved 2013-09-25.
  4. ^ Escaping single-quotes within single-quoted strings
  5. ^ Examples of passing arguments given to a command alias Archived 2012-11-25 at the Wayback Machine

External links

Alias

Alias or aliasing may refer to:

Alias name, a pseudonym

Anonymous function

In computer programming, an anonymous function (function literal, lambda abstraction, or lambda expression) is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions, or used for constructing the result of a higher-order function that needs to return a function.

If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types.

Anonymous functions originate in the work of Alonzo Church in his invention of the lambda calculus, in which all functions are anonymous, in 1936, before electronic computers. In several programming languages, anonymous functions are introduced using the keyword lambda, and anonymous functions are often referred to as lambdas or lambda abstractions. Anonymous functions have been a feature of programming languages since Lisp in 1958, and a growing number of modern programming languages support anonymous functions.

DBase

dBase (also stylized dBASE) was one of the first database management systems for microcomputers, and the most successful in its day. The dBase system includes the core database engine, a query system, a forms engine, and a programming language that ties all of these components together. dBase's underlying file format, the .dbf file, is widely used in applications needing a simple format to store structured data.

dBase was originally published by Ashton-Tate for microcomputer operating system CP/M in 1980, and later ported to Apple II and IBM PC computers running DOS. On the PC platform, in particular, dBase became one of the best-selling software titles for a number of years. A major upgrade was released as dBase III, and ported to a wider variety of platforms, adding UNIX, and VMS. By the mid-1980s, Ashton-Tate was one of the "big three" software publishers in the early business software market, the others being Lotus Development and WordPerfect.

Starting in the mid-1980s, several companies produced their own variations on the dBase product and especially the dBase programming language. These included FoxBASE+ (later renamed FoxPro), Clipper, and other so-called xBase products. Many of these were technically stronger than dBase, but could not push it aside in the market. This changed with the disastrous introduction of dBase IV, whose design and stability were so poor that many users switched to other products. At the same time, there was growing use of IBM-invented SQL (Structured Query Language) in database products. Another factor was user adoption of Microsoft Windows on desktop computers. The shift toward SQL and Windows put pressure on the makers of xBase products to invest in major redesign to provide new capabilities.

In the early 1990s xBase products constituted the leading database platform for implementing business applications. The size and impact of the xBase market did not go unnoticed, and within one year, the three top xBase firms were acquired by larger software companies:

Borland purchased Ashton-Tate

Microsoft bought Fox Software, and

Computer Associates acquired Nantucket.By the following decade most of the original xBase products had faded from prominence and several disappeared. Products known as dBase still exist, owned by dBase LLC.

Tcsh

tcsh ( “tee-see-shell”, “tee-shell”, or as “tee see ess aitch”) is a Unix shell based on and compatible with the C shell (csh). It is essentially the C shell with programmable command-line completion, command-line editing, and a few other features. Unlike the other common shells, functions cannot be defined in a tcsh script and the user must use aliases instead (as in csh). It is the native root shell for BSD-based systems such as FreeBSD.

tcsh added filename and command completion and command line editing concepts borrowed from the Tenex system, which is the source of the “t”. Because it only added functionality and did not change what was there, tcsh remained backward compatible with the original C shell. Though it started as a side branch from the original csh source tree that Bill Joy had created, tcsh is now the main branch for ongoing development. tcsh is very stable but new releases continue to appear roughly once a year, consisting mostly of minor bug fixes.On many systems, such as Mac OS X and Red Hat Linux, csh is actually tcsh. Often one of the two files is either a hard link or a symbolic link to the other, so that either name refers to the same improved version of the C shell.

On Debian and some derivatives (including Ubuntu), there are two different packages: csh and tcsh. The former is based on the original BSD version of csh and the latter is the improved tcsh.

File system
Processes
User environment
Text processing
Shell builtins
Searching
Documentation
Software development
Miscellaneous

This page is based on a Wikipedia article written by authors (here).
Text is available under the CC BY-SA 3.0 license; additional terms may apply.
Images, videos and audio are available under their respective licenses.