教科书告诉我,用bash进行正则表达式比较,用如下方式:
if [[ "$1" =~ '^[[:space:]]*#' ]]
然而将同样语句移植到cygwin中会发生死活认不出的情况。研究了一下,将语句作如下修改:
if [[ $1 =~ ^[[:space:]]*# ]]
即可。
原因如下:
In bash 3.2, the [[ ]] quoting rules changed slightly. Since [[ already
introduces special quoting rules, single quotes are not required; the
regex is already protected by the shell from globbing.
本文介绍了如何在Bash中使用正则表达式进行字符串匹配,并对比了Bash3.2版本前后对于特殊字符的不同处理方式。通过一个具体的示例说明了在不同环境下(如Cygwin)进行移植时需要注意的问题。
2891

被折叠的 条评论
为什么被折叠?



