你可以使用数组
match
而不是
$BASH_REMATCH
. 你也可以避开额外的闭合花括号。
未经测试的:
function parse_git_branch {
git_status="$(git status 2> /dev/null)"
pattern="^# On branch ([^${IFS}]*)"
if [[ ! ${git_status}\} =~ "working directory clean" ]]; then
state="*"
fi
# add an else if or two here if you want to get more specific
if [[ ${git_status} =~ ${pattern} ]]; then
branch=${match[1]}
echo "(${branch}${state})"
fi
}
试一试,看看是否有帮助。