bash 别名中允许使用哪些字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24690640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:50:57  来源:igfitidea点击:

Which characters are allowed in a bash alias

linuxmacosbash

提问by Boris

I recently added

我最近添加了

alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'

to my bash_aliases file. Playing around with this, I noticed that while aliasing ..was allowed, aliasing .was not. What other punctuation characters are allowed in bash aliases?

到我的 bash_aliases 文件。玩弄这个,我注意到虽然..允许混叠,但不允许混叠.。bash 别名中还允许使用哪些其他标点符号?

采纳答案by Boris

Bash aliases definitely cannot contain these characters: space , tab \t, newline \n, /, $, `, =, |, &, ;, (, ), <, >, 'and ".

巴什别名绝对不能包含这些字符:空格,制表\t符,换行符\n/$`=|&;()<>'"

The GNU manual for bash https://www.gnu.org/software/bash/manual/html_node/Aliases.htmlsays:

bash 的 GNU 手册https://www.gnu.org/software/bash/manual/html_node/Aliases.html说:

The characters /, $, `, =and any of the shell metacharacters or quoting characters listed above may not appear in an alias name.

字符/$`=和任何壳或元字符上面列出的引用字符的可能不会出现在一个别名。

"shell metacharacters" are defined in https://www.gnu.org/software/bash/manual/html_node/Definitions.htmlas

“shell 元字符”在https://www.gnu.org/software/bash/manual/html_node/Definitions.html中定义为

A character that, when unquoted, separates words. A metacharacter is a space, tab, newline, or one of the following characters: |, &, ;, (, ), <, or >.

不加引号时用于分隔单词的字符。元字符是一个spacetabnewline,或以下字符中的一个:|&;()<,或>

I couldn't find a list of "quoting characters" https://www.gnu.org/software/bash/manual/html_node/Quoting.html. As far as I know they are the single 'and double "quotes.

我找不到“引用字符”列表https://www.gnu.org/software/bash/manual/html_node/Quoting.html。据我所知,它们是单引号'和双"引号。

回答by konsolebox

It's always been safer to use functions instead:

使用函数总是更安全:

function .. { cd '..'; }
function ... { cd '../..'; }
function .... { cd '../../..'; }

Or for a conservative preference:

或者对于保守的偏好:

..() { cd '..'; }
...() { cd '../..'; }
....() { cd '../../..'; }

What is safer about doing it this way?

这样做更安全吗?

Aliases affect first-class syntax (if, case, etc.) and its commands are specified using quotes. It's like having to use eval unnecessarily. Using functions on the other is straightforward and you know exactly how the arguments are expanded. TL;DR aliases are a hack. It's like using a #define statement in C when you can simply define a function. It's something beginners would love to compose.

别名影响一流的语法(if、case 等),它的命令使用引号指定。这就像不必要地使用 eval 一样。另一方面,使用函数很简单,您确切地知道参数是如何展开的。TL;DR 别名是一个黑客。当您可以简单地定义一个函数时,这就像在 C 中使用 #define 语句一样。这是初学者喜欢创作的东西。

If anyone would want to define default arguments to a command, you can use command to make the function call the external command to avoid recursion. That's the right way to do it. E.g. function grep { command grep --color=auto "$@"; }

如果有人想为命令定义默认参数,您可以使用 command 使函数调用外部命令以避免递归。这是正确的做法。例如function grep { command grep --color=auto "$@"; }

回答by Lime

According to the followingenvironment variables names can only contain

根据以下环境变量名称只能包含

uppercase letters, digits, and the '_' (underscore) from the characters defined in Portable Character Set and do not begin with a digit. Other characters may be permitted by an implementation; applications shall tolerate the presence of such names.

可移植字符集中定义的字符中的大写字母、数字和“_”(下划线),并且不以数字开头。实现可能允许其他字符;应用程序应容忍此类名称的存在。

But variables are not really the same the same of alias names.

但是变量与别名实际上并不相同。

In practice a -(dash) should be added to the list because it is a defacto standard in debian(apt-get). In addition _~@#%^.,all work and ><'"|=()backtick/?[]+!. don't. In addition you have to worry about $PATHand characters like :. It appears space and linebreaks could work if they are properly escaped or quoted like so "a b".

实际上,-应该将(破折号)添加到列表中,因为它是 debian( apt-get) 中的事实标准。此外_~@#%^.,所有的工作和><'"|=()backtick/?[]+!. 别。另外你还要担心$PATH和人物一样:。如果像这样正确转义或引用空格和换行符,它们似乎可以工作"a b"

回答by DTSCode

https://www.gnu.org/software/bash/manual/html_node/Aliases.htmlI wouldn't name an alias .. in linux . is a special name for the current director and .. is for the parent directory. there could be some obscure process that could be screwed up by aliasing .. for cd

https://www.gnu.org/software/bash/manual/html_node/Aliases.html我不会在 linux 中命名别名 .. 。是当前目录的特殊名称,.. 是父目录的名称。可能有一些晦涩的过程可能会被混叠搞砸 .. for cd