Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

alias (command)

From Wikipedia, the free encyclopedia
Shell command that defines a word that acts like a command
For the aliasing of variables in a programming language, seeAliasing (computing).
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Alias" command – news ·newspapers ·books ·scholar ·JSTOR
(July 2013) (Learn how and when to remove this message)
alias
Example ofalias command
Original authorBill Joy
DevelopersVariousopen-source andcommercial developers
Operating systemUnix,Unix-like,AmigaDOS,FreeDOS,Microsoft Windows,ReactOS,AROS,KolibriOS,IBM i
PlatformCross-platform
TypeCommand

alias is ashellcommand that defines a word that the shell replaces with associated text before interpreting a command line.[1] It is often used to enhance productivity by abbreviating a command or for including commonly used arguments with a command. The command is available inUnix shells,AmigaDOS,4DOS/4NT,FreeDOS,KolibriOS,PowerShell,ReactOS,EFI shell,[2] andIBM i.[3] Aliasing functionality inMS-DOS andCommand Prompt is provided by theDOSKEY command.

Since aliases are defined only for a shell session, regularly used aliases are often defined in a session startupshell script such as.bashrc. Thealias commands may either be written in the config script directly orsourced from a separate file.

Aliases were introduced in theC shell to survive in descendant shells such astcsh andbash. As these aliases were limited to one line they were useful for creating relatively simple shortcut commands, but not more complex constructs. Older versions of theBourne shell did not offer aliases, but did provide functions, which are more powerful than the csh alias. Eventually, the csh alias was implemented in thebash andksh shells. With shells that support both functions and aliases but no parameterized inline shell scripts, the use of functions wherever possible is recommended. Nonetheless, aliases are necessary where chained aliases are required.

Features

[edit]

Define

[edit]

The following is an example that definesgc to be a command the performs the actiongit commit.

aliasgc='git commit'

In C shell and tcsh there is no equals sign:

aliasgc"git commit"

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

new-aliascicopy-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, theset verb is used to change an existing alias. The following changes the aliasci to invoke thecls command.

set-aliascicls

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

eset /a cp

List

[edit]

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:

aliasmyAlias

Remove

[edit]

In Unix shells and 4DOS/4NT, aliases can be removed viaunalias. To remove thecopy alias:

unaliascopy

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 thealias:\ drive viaremove-item:

remove-itemalias:ci

Ignore

[edit]

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

aliasls='ls -la'

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

alias dir = *dir /2/p

Chaining

[edit]

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

For example, the following defineslist to invokels andlong to as a set ofls options. The command alias must end with a space to enable chaining.

aliaslist='ls 'aliaslong='-Flas'

Then, command linelist long myfile expands tols -Flas myfile.

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

Command arguments

[edit]

In theC Shell,arguments can be embedded inside the command using the string\!*. For example, with this alias:

aliasls-more'ls \!* | more'

ls-more /etc /usr expands tols /etc /usr | more to list the contents of the directories /etc and /usr, pausing after every screenful. Without\!*,

aliasls-more'ls | more'

would instead expand tols | more /etc /usr which incorrectly attempts to open the directories inmore.[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

[edit]

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'sPATH (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 internalruntime environment of the shell (such asenvironment variables), needs to change the shell'sworking 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 ofrm,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, aliasalias ll='ls -Flas' can be implemented as functionll () { ls -Flas "$@" ;}. To prevent a function fromcalling itself, usecommand:ls () { command ls --color=auto "$@" ; }. In older Bourne shells, use/bin/ls instead ofcommand ls.

References

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

Further reading

[edit]

External links

[edit]
The WikibookGuide to Unix has a page on the topic of:Commands
File system
Processes
User environment
Text processing
Shell builtins
Searching
Documentation
Software development
Miscellaneous
Retrieved from "https://en.wikipedia.org/w/index.php?title=Alias_(command)&oldid=1317090511"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp