Regex match a string with spaces (use quotes?) in an if statement

后端 未结 4 1344
情书的邮戳
情书的邮戳 2021-02-19 11:05

How would I do a regex match as shown below but with quotes around the (\"^This\") as in the real world \"This\" will be a string that can have spaces in it.

#!/         


        
4条回答
  •  清酒与你
    2021-02-19 11:19

    You can use \ before spaces.

    #!/bin/bash
    
    text="This is just a test string"
    if [[ "$text" =~ ^This\ is\ just ]]; then
      echo "matched"
    else
      echo "not matched"
    fi
    

提交回复
热议问题