Bash 别名绝对不能包含这些字符:空格
, tab \t
, 新队\n
, /
, \
, $
, `
, =
, |
, &
, ;
, (
, )
, <
, >
, '
and "
.
Bash 参考手册says https://www.gnu.org/software/bash/manual/html_node/Aliases.html:
那些角色/
, $
, `
, =
并且上面列出的任何 shell 元字符或引用字符都不能出现在别名中。
“Shell 元字符”是defined https://www.gnu.org/software/bash/manual/html_node/Definitions.html as
当不加引号时,用于分隔单词的字符。元字符是一个space
, tab
, newline
,或以下字符之一:|
, &
, ;
, (
, )
, <
, or >
.
“引用字符”不是明确列出 https://www.gnu.org/software/bash/manual/html_node/Quoting.html但它们是反斜杠\
和单身'
和双"
quotes.
Bash 检查别名in legal_alias_name() https://github.com/bminor/bash/blob/ec8113b9861375e4e17b3307372569d429dec814/general.c#L423-L424这是在这里实现的:1 https://github.com/bminor/bash/blob/ec8113b9861375e4e17b3307372569d429dec814/syntax.h#L72, 2 https://github.com/bminor/bash/blob/ec8113b9861375e4e17b3307372569d429dec814/syntax.h#L74, 3 https://github.com/bminor/bash/blob/ec8113b9861375e4e17b3307372569d429dec814/syntax.h#L83-L87。查看输出mksyntax.c https://github.com/bminor/bash/blob/master/mksyntax.c(生成另一个 C 程序的 C 程序)在我的系统上,shellbreak()
将为这 10 个字符返回 true:\t\n &();<>|
, shellxquote()
对于这 4 个:"'\`
, and shellexp()
for $
, 最后legal_alias_name()
还检查/
.
然而,还有其他限制:1)Bash 如何标记化alias
您必须编写来定义别名的表达式,以及 2) 它将如何标记以别名开头的命令。即,对于 1),=
不能出现在别名中,可能是因为alias foo=bar=baz
将被解析为foo
= bar=baz
, not foo=bar
= baz
。对于2),根据我的实验,您可以定义一个别名!
, the 历史扩展 https://www.gnu.org/software/bash/manual/html_node/History-Interaction.html字符,但如果你尝试使用它,你会得到一个command not found
错误,除非!
位于命令的末尾。可能还有其他字符,最安全的选择是坚持使用数字、字母和下划线。