什么是以下脚本中的~~

时间:2013-08-07 07:48:47

标签: bash shell

此脚本检查目录是否是另一个目录的父目录。如何使用这个脚本?

     T() {
          if [[ "$2" =~ ${1}['/'?] ]] ; then
              echo "$2 is child of $1"
              return 0
          else
              echo "$2 is NOT child of $1 ($?)"
              return 1
          fi
         }

3 个答案:

答案 0 :(得分:2)

它是一个正则表达式匹配,是一个将字符串与模式匹配的有用工具。 This link可能会对您有所帮助。

答案 1 :(得分:2)

它是正则表达式模式匹配运算符。请参阅Conditional constructs下的bash参考手册 - 并查找[[ ... ]]构造。

答案 2 :(得分:1)

来自手册:

An additional binary operator, ‘=~’, is available, with the same precedence as ‘==’ and ‘!=’. When 
it is used, the string to the right of the operator is considered an extended regular expression 
and matched accordingly (as in regex3)). The return value is 0 if the string matches the pattern, 
and 1 otherwise.