用戶:aleiPhoenix/alias (Unix)
在計算機領域, alias(英語,別名的意思)在命令行解釋器、shell、4DOS/4NT和Windows PowerShell里是一個命令,採用一個單詞來替代一串字符串。它主要被用來縮略系統命令,或者為常用的命令添加默認的參數。MS-DOS與Microsoft 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"
alias copy "cp"
這個別名意味着當shell中接收到copy
命令,則會被替換為cp
並加以執行。
下面這個命令語法則表示在4DOS/4NT中將cp
定義為4DOS命令copy
的別名:
alias cp copy
在Windows PowerShell中創建一個別名,可以連同動詞new
與名為alias
的cmdlet一起使用:
new-alias ci copy-item
這句命令創建了一個叫做ci
的別名來替代原本執行copy-item
的cmdlet。
歷史
在Unix中,別名首次在C Shell中被引入並且成功在tsch和Bash中沿襲下來。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 "$@" ; }
External links
- 單一UNIX®規範第7期,由國際開放標準組織發佈 : define or display aliases – 命令與工具(Commands & Utilities)參考,
- Bash man page for alias
- The alias Command by The Linux Information Project (LINFO)