Bash脚本正则表达式匹配无法转义双引号

时间:2015-04-10 05:00:17

标签: regex bash shell

我尝试了尝试实现以下目标的不同变体,但我不能。

#!/bin/bash

sb_command_passed=$*
regexMatch="^find every file matching '(.*)' in this folder and subfolders"
[[ $sb_command_passed =~ $regexMatch ]]
param1=${BASH_REMATCH[1]}
echo $param1

现在将以下内容传递给脚本:

find every file matching "search" in this folder and subfolders

它不会将搜索作为匹配参数返回。如果成功,我想在那里传递正则表达式:

find every file matching "\s.+[^&*]" in this folder and subfolders

并获得 \ s。+ [^& *] 作为回报。你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

看起来你只是错误地引用你的报价。这有效:

$ regexMatch='^find every file matching "(.*)" in this folder and subfolders'
$ sb_command_passed='find every file matching "\s.+[^&*]" in this folder and subfolders'
$ [[ $sb_command_passed =~ $regexMatch ]] && echo "${BASH_REMATCH[1]}"
\s.+[^&*]

单引号围绕整个字符串,双引号围绕您要捕获的部分。