User:aleiPhoenix/alias (Unix)

计算机领域, alias(英语,别名的意思)在命令行解释器shell4DOS/4NTWindows PowerShell里是一个命令,采用一个单词来替代一串字符串。它主要被用来缩略系统命令,或者为常用的命令添加默认的参数。MS-DOSMicrosoft Windows操作系统 中的别名功能则是又DOSKey命令行工具提供的。

一个别名会在shell会话中持续到结束为止。常用的别名都可以在shell的配置文件(对于csh来说是"~/.cshrc"或者系统级别的"/etc/csh.rc",对于bash来说则是"~/.bashrc"或者系统级别的"/etc/bashrc")设置,以在响应的shell会话启动时即可使用。别名命令既可以直接写在配置文件里或者使用source命令从另外一个单独的文件引用,名字一般是.alias(如果有使用多个shell,也可能是.alias-bash、.alias-csh等等)。

创建别名

编辑

可以通过给alias命令传递"名字/值"的成对参数来创建别名。Bash中的语法示例如下:

alias copy="cp"

C shelltcsh中响应的命令则如下:

alias copy "cp"

这个别名意味着当shell中接收到copy命令,则会被替换为cp并加以执行。

下面这个命令语法则表示在4DOS/4NT中将cp定义为4DOS命令copy的别名:

alias cp copy

在Windows PowerShell中创建一个别名,可以连同动词new与名为aliascmdlet一起使用:

new-alias ci copy-item

这句命令创建了一个叫做ci的别名来替代原本执行copy-item的cmdlet。

历史

编辑

在Unix中,别名首次在C Shell中被引入并且成功在tschBash中沿袭下来。C shell中的别名被严格限制为只能使用一行,而shell语言中复杂的结构则完全不能满足,然而即使如此alias命令仍旧对创建简单的快捷命令很有用。别名却在工具繁多、功能强大的Bourne shell中缺席。别名的概念被导入到Bourne Again Shell(bash)与Korn shell(ksh)中。这类shell中,同时支持函数与别名,而更推荐使用函数。此外bash与ksh有必要的情况下,还可以使用别名的嵌套。

查看已定义的别名

编辑

如欲查看已经定义的别名,可以使用如下的命令:

alias          # 不带任何参数;列出所有已定义的别名
alias -p       # 与上一条类似;在4DOS/4NT和PowerShell中不可用
alias myAlias  # 显示单条定义的别名内容

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