What does the “=~” operator do in shell scripts?

后端 未结 4 1972
别那么骄傲
别那么骄傲 2020-12-29 06:48

It seems that it is sort of comparison operator, but what exactly it does in e.g. the following code (taken from https://github.com/lvv/git-prompt/blob/master/git-prompt.sh#

4条回答
  •  情书的邮戳
    2020-12-29 07:11

    It matches regular expressions

    Refer to following example from http://tldp.org/LDP/abs/html/bashver3.html#REGEXMATCHREF

    #!/bin/bash
    
    input=$1
    
    
    if [[ "$input" =~ "[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ]]
    #                 ^ NOTE: Quoting not necessary, as of version 3.2 of Bash.
    # NNN-NN-NNNN (where each N is a digit).
    then
      echo "Social Security number."
      # Process SSN.
    else
      echo "Not a Social Security number!"
      # Or, ask for corrected input.
    fi
    

提交回复
热议问题