Correct regex doesn't work in bash
Positive lookbehind ((?<=…)
) does not work in POSIX ERE used by [[ … =~ … ]]
.
In your use case you can match a broader pattern and remove the excessive part later:
#!/bin/bash
python_version=$(python3 --version)
regexp='^Python 3\.[0-9]+'
[[ $python_version =~ $regexp ]]
echo "${BASH_REMATCH#Python }"
Note this approach wouldn't work for arbitrary regex instead of your ^Python
. In ${var#pattern}
the pattern is like filename generation pattern, not a regex. See this another answer of mine.