正则表达式在if语句中匹配带空格的字符串(使用引号?)

Min*_*int 13 regex linux string bash debian

我如何进行正则表达式匹配,如下所示,但在("^ This")周围引用,如在现实世界中"This"将是一个可以在其中包含空格的字符串.

#!/bin/bash

text="This is just a test string"
if [[ "$text" =~ ^This ]]; then
 echo "matched"

else
 echo "not matched"
fi
Run Code Online (Sandbox Code Playgroud)

我想做点什么

    if [[ "$text" =~ "^This is" ]]; then
Run Code Online (Sandbox Code Playgroud)

但这不匹配.

too*_*php 20

您可以\在空格之前使用.

#!/bin/bash

text="This is just a test string"
if [[ "$text" =~ ^This\ is\ just ]]; then
  echo "matched"
else
  echo "not matched"
fi
Run Code Online (Sandbox Code Playgroud)