当使用$'\x01'
End-Of-String char 时,Bash 正则表达式无法正确比较。$
所有其他字节值(似乎)比较正确。
使用 GNU bash 4.1.5(1)。这是一个错误,还是除了 之外还有其他方法以十六进制表示法表示字节$'\...'
? ...但这似乎不是符号,因为即使是文字 char 到文字 char 的比较也会失败。
$'\x01'
仅当紧接在 End-Of-String 之前时才会发生此“失败” $
。
这里有些例子:
echo 'non \x01 with ^ and $'
[[ 3 =~ ^$'\x33'$ ]]; echo $? # 0
[[ 3 =~ ^$'\063'$ ]]; echo $? # 0
[[ $'\x12' =~ ^$'\x12'$ ]]; echo $? # 0
[[ $'\002' =~ ^$'\x02'$ ]]; echo $? # 0
echo '\x01 with no ^ or $'
[[ $'\x01' =~ $'\x01' ]]; echo $? # 0
[[ $'\x01' =~ $'\001' ]]; echo $? # 0
[[ =~ $'\001' ]]; echo $? # 0 nb. Literal char does not render
[[ =~ ]]; echo $? # 0 nb. Literal char does not render
echo '\x01 with ^ only'
[[ $'\x01' =~ ^$'\x01' ]]; echo $? # 0
[[ $'\x01' =~ ^$'\001' ]]; echo $? # 0
[[ =~ ^$'\001' ]]; echo $? # 0 nb. Literal char does not render
[[ =~ ^ ]]; echo $? # 0 nb. Literal char does not render
echo '\x01 with ^ and $'
[[ $'\x01' =~ ^$'\x01'$ ]]; echo $? # 1
[[ $'\x01' =~ ^$'\001'$ ]]; echo $? # 1
[[ =~ ^$'\001'$ ]]; echo $? # 1 nb. Literal char does not render
[[ =~ ^$ ]]; echo $? # 1 nb. Literal char does not render
echo '\x01 with $ only'
[[ $'\x01' =~ $'\x01'$ ]]; echo $? # 1
[[ $'\x01' =~ $'\001'$ ]]; echo $? # 1
[[ =~ $'\001'$ ]]; echo $? # 1 nb. Literal char does not render
[[ =~ $ ]]; echo $? # 1 nb. Literal char does not render
echo '\x01 with $ only, but not adjacent to \x01'
[[ $'\x01'c =~ $'\x01'c$ ]]; echo $? # 0
[[ $'\x01'c =~ $'\001'c$ ]]; echo $? # 0
[[ c =~ $'\001'c$ ]]; echo $? # 0 nb. Literal char does not render
[[ c =~ c$ ]]; echo $? # 0 nb. Literal char does not render
答案1
是的,这是旧版本中的一个错误,已bash
在 bash-4.2.14 中修复
这是使问题消失的提交;你可以随心所欲地利用它。
什么是CTLESC
?你看,它是在syntax.h
as中定义的。#define CTLESC '\001'
这是某种与扩张有关的内部逃避。看起来这个错误可能是你的\x01
数据被解释为好像它是内部生成的CTLESC
或类似的东西。
commit 25db9a70d4c2ba5c43d4167f231bdd8d760d5a06
Author: Chet Ramey <[email protected]>
Date: Tue Nov 22 20:02:46 2011 -0500
Bash-4.2 patch 14
diff --git a/patchlevel.h b/patchlevel.h
index 636be1c..04b423b 100644
--- a/patchlevel.h
+++ b/patchlevel.h
@@ -25,6 +25,6 @@
regexp `^#define[ ]*PATCHLEVEL', since that's what support/mkversion.sh
looks for to find the patch level (for the sccs version string). */
-#define PATCHLEVEL 13
+#define PATCHLEVEL 14
#endif /* _PATCHLEVEL_H_ */
diff --git a/pathexp.c b/pathexp.c
index 42f21e4..f239956 100644
--- a/pathexp.c
+++ b/pathexp.c
@@ -196,7 +196,7 @@ quote_string_for_globbing (pathname, qflags)
{
if ((qflags & QGLOB_FILENAME) && pathname[i+1] == '/')
continue;
- if ((qflags & QGLOB_REGEXP) && ere_char (pathname[i+1]) == 0)
+ if (pathname[i+1] != CTLESC && (qflags & QGLOB_REGEXP) && ere_char (pathname[i+1]) == 0)
continue;
temp[j++] = '\\';
i++;