Menu
Home Explore People Places Arts History Plants & Animals Science Life & Culture Technology
On this page
alias (command)
Shell command that defines a word that acts like a command

An alias is a shell command that substitutes a word with associated text before execution, enhancing productivity by abbreviating commands or including common arguments. Aliases are supported in many environments such as Unix shells, PowerShell, FreeDOS, and EFI shell. In MS-DOS and Command Prompt, aliasing is provided via the DOSKEY command. Since aliases last only per session, they're often defined in startup shell scripts like .bashrc, either directly or by being sourced from files. Originally introduced with the C shell, aliases are limited to one line and coexist with functions in shells like bash and ksh, where functions are preferred for complexity but aliases remain useful for chaining commands.

Related Image Collections Add Image
We don't have any YouTube videos related to alias (command) yet.
We don't have any PDF documents related to alias (command) yet.
We don't have any Books related to alias (command) yet.
We don't have any archived web articles related to alias (command) yet.

Features

Define

The following is an example that defines gc to be a command the performs the action git commit.

alias gc='git commit'

In C shell and tcsh there is no equals sign:

alias gc "git commit"

To define an alias in PowerShell, the new-alias cmdlet is used:

new-alias ci copy-item

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.

In PowerShell, the set verb is used to change an existing alias. The following changes the alias ci to invoke the cls command.

set-alias ci cls

In 4DOS/4NT shell, the eset command provides an interactive command line to edit an existing alias. For example:

eset /a cp

List

To view defined aliases:

alias

To list aliases in a way that allows for re-creating them by sourcing the output (not available in 4DOS/4NT or PowerShell):

alias -p

To report the definition of a particular alias name:

alias myAlias

Remove

In Unix shells and 4DOS/4NT, aliases can be removed via unalias. To remove the copy alias:

unalias copy

To remove all aliases (not available in 4DOS/4NT):

unalias -a

To remove all aliases in 4DOS/4NT:

unalias *

In PowerShell, an alias is removed from the alias:\ drive via remove-item:

remove-item alias:ci

Ignore

In Unix shells, an aliased word can be used without replacement by using quotes. For example, consider the following command that defines an alias ls that invokes the original ls with options -la. To invoke the original ls command (without the options), the following syntax is used: 'ls' or \ls.

alias ls='ls -la'

In 4DOS/4NT shell, an asterisk is used. For example, the following defines dir to invoke the original dir (requires asterisk in the definition) with options /2/p. To later invoke the original dir, the syntax is *dir.

alias dir = *dir /2/p

Chaining

Typically, aliases are used to replace the first word of a command line, but some shells such as bash and ksh also support chaining – replacing subsequent words.

For example, the following defines list to invoke ls and long to as a set of ls options. The command alias must end with a space to enable chaining.

alias list='ls ' alias long='-Flas'

Then, command line list long myfile expands to ls -Flas myfile.

The behavior provided by chaining is not possible via shell functions.

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

Some shells such as bash and ksh do not support this syntax, but do provide for similar functionality via shell functions — see § Alternatives below.

Alternatives

Best practice is to only define an alias for a relatively simple command. Alternatives for more complicated logic include:

  • Shell script, provides a rich ability to implement a command
  • Symbolic link in the user's PATH (such as /bin); 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 function, especially if the command being created needs to modify the internal runtime environment of the shell (such as environment variables), needs to change the shell's working directory, or must be implemented in a way which guarantees that it appear in the command search path for anything but an interactive shell (especially any "safer" version of rm, cp, mv and so forth)

A relatively simple alias that includes a few arguments and supports subsequent arguments, can be converted to a shell function in a relatively straightforward process. For example, alias alias ll='ls -Flas' can be implemented as function ll () { ls -Flas "$@" ;}. To prevent a function from calling itself, use command: ls () { command ls --color=auto "$@" ; }. In older Bourne shells, use /bin/ls instead of command ls.

Further reading

  • McElhearn, Kirk (2006). The Mac OS X Command Line: Unix Under the Hood. John Wiley & Sons. ISBN 978-0470113851.
The Wikibook Guide to Unix has a page on the topic of: Commands

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

  2. "EFI Shells and Scripting". Intel. Retrieved 2013-09-25. http://software.intel.com/en-us/articles/efi-shells-and-scripting/

  3. IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). IBM. Retrieved 2020-09-05. /wiki/IBM

  4. "Examples of passing arguments given to a command alias". UNIXhelp. University of Edinburgh. Archived from the original on 2012-11-25. https://web.archive.org/web/20121125074502/http://unixhelp.ed.ac.uk/shell/alias_csh2.1.html