是一个无效的Bash别名

时间:2016-12-13 12:59:57

标签: bash unicode

在Mac OS X上我向我的str.match([a-zA-Z]+(?: |,)*[0-9]{1,2}(?: |,)*[0-9]+$) 添加别名。首先我使用,但因为它是一个构建命令,我发现更合适。

我试过这些:

.bash_profile

奇怪的是,这不是一个有效的别名,正如bash报道:

alias ='echo "build with fun."'
alias ='echo "build with tools."'
alias ='echo "build with style."'

为什么会这样??

2 个答案:

答案 0 :(得分:4)

shell想要出于某种原因引用该名称,并且相应地禁止它作为别名。 general.c:legal_alias_name()

即使locale输出' en_US.UTF-8'所有价值观。

尝试使用其他表情符号:

~$ alias ='echo oh bother'
~$ 
oh bother
~$ alias ='echo build'
bash: alias: `': invalid alias name
--> 1

shell为什么要引用该名称?我的第一个猜测来自Unicode 7.0,而来自Unicode 6.0,我使用ls的行为来说服自己:

~$ touch  
~$ ls -b  
                \360\237\233\240

因为我可以在Linux上使用别名而不是macOS,所以这看起来特别好,因为macOS附带了旧版本的bash - 即使我使用自制软件安装了最新版本。

为了测试这个理论,我尝试了,同样来自Unicode 6.0:

$ alias ,='echo choo choo'
bash: alias: `,': invalid alias name
--> 1

所以不是这样! Linux上的bash很乐意将所有这些字符用作别名。 (并且ls的行为是一个红色的鲱鱼,因为它将打印(7.0)为utf-8但Mac上的bash会接受它作为别名。)

但别名名称函数似乎将字符串视为 bytes ,而不是字符,并且这两种失败都以0xA0结尾,我知道这是非破碎的空间'在Latin-1中。没有跳到所有C宏声明,我敢打赌这个角色会被怀疑。

Mac$ alias $'\u00a0'=foo 
bash: alias: ` ': invalid alias name
--> 1
Linux$ alias $'\u00a0'=foo
Linux$ $'\u00a0'
 : command not found
--> 127

显然,选择一个UTF-8编码不包含A0字节的字符。但我不知道为什么在我的Linux机器上忽略了这个检查。

答案 1 :(得分:1)

你需要反斜杠\来忽略特殊含义

  

alias \ =' echo"有趣的构建。"'
别名\ =' echo" build with   工具"'
别名\ =' echo"以风格建造。"'

<强> 试验:

$  tes*
test  test_script.sh  test.sh  test.xml
$grep '' .bash_profile
  

alias \ =&#39; ls&#39;

相关问题