emanuele6 1658847028 learn #redirect bash51localrematch emanuele6 1675766275 forget emanuele6 1675766345 learn somefun () { local obr=${BASH_REMATCH[*]@A}; if [[ $obr ]]; then if [[ ${BASH_REMATCH@a} ]]; then obr=${obr/-/-g}; else obr='declare -g '$obr; fi elif declare -p BASH_REMATCH > /dev/null 2>&1; then obr='declare -g BASH_REMATCH'; fi; ...; [[ $str =~ $re ]]; ...; unset -v BASH_REMATCH; eval " $obr" ;} emanuele6 1675766387 forget emanuele6 1675766708 learn The [[ =~ ]] condition sets BASH_REMATCH on the global scope. Before Bash 5.1, BASH_REMATCH was readonly and could not be localised. In Bash 5.1, and Bash 5.2, BASH_REMATCH is no longer readonly, so it can be unset and restored using the technique described in !bash51localrematch . In Bash 5.3, you will be able to just use local BASH_REMATCH .