要使用shell正则表达式来匹配字符串,可以使用以下方法:
-
使用grep命令:
echo"helloworld"|grep"hello"
输出:
helloworld
-
使用expr命令:
string="helloworld" expr"$string":'hello'
输出:
hello
-
使用[[]]表达式:
string="helloworld" if[[$string=~"hello"]];then echo"匹配成功" fi
输出:
匹配成功
-
使用case语句:
string="helloworld" case"$string"in *hello*)echo"匹配成功";; esac
输出:
匹配成功
这些方法都可以用来匹配字符串,具体使用哪种方法取决于你的需求和上下文。