如何使用 bash 将提交消息解析为变量?

how to parse commit message into variables using bash?

我使用 bash 并且我有字符串(提交消息)

:sparkles: feat(xxx): this is a commit

我想把它分成变量部分:

emoji=:sparkles:
type=feat
scope=xxx
message=this is a commit

我尝试使用 grep,但正则表达式不是 return 我需要的(例如 "type"),无论如何如何将它粘贴到变量中?

echo ":sparkles: feat(xxx): this is a commit" | grep "[((.*))]"

使用 bash 版本 >= 3,一个正则表达式和一个数组:

x=":sparkles: feat(xxx): this is a commit"

[[ "$x" =~ ^(:.*:)\ (.*)\((.*)\):\ (.*)$ ]]
echo "${BASH_REMATCH[1]}"
echo "${BASH_REMATCH[2]}"
echo "${BASH_REMATCH[3]}"
echo "${BASH_REMATCH[4]}"

输出:

:sparkles:
feat
xxx
this is a commit

来自man bash

BASH_REMATCH: An array variable whose members are assigned by the =~ binary operator to the [[ conditional command. The element with index 0 is the portion of the string matching the entire regular expression. The element with index n is the portion of the string matching the nth parenthesized subexpression. This variable is read-only.