Issue
I have CI/CD Config which required python version to be set to default by pyenv. I want to python2 -V
output showed up with only, example, 2.7.18. But, rather than showing 2.7.18, it showing full text Python 2.7.18
.
But, when I use it in python3 python -V
, it showed the correct & current python3 version (3.9.0
).
I use this code to try showing numbers only : $(python -V | grep -Eo '[0-9]\.[0-9]\.[10-19]')
.
And to set default with pyenv : pyenv global $(python3 -V | grep -Eo '[0-9]\.[0-9]\.[10-19]') $(python -V | grep -Eo '[0-9]\.[0-9]\.[10-19]')
So pyenv $(python3 version) $(python2 version)
Here is the image :
Thanks!
Solution
A simple way would be to just replace the string Python with the emtpy string, if it exists.
Here a quick one-liner
python -V 2>&1| sed -e "s/Python//g" | xargs
That would print the python version, redirects stderr to stdout, replaces "Python" with "". Xargs without parameters returns the trimmed input string.
Answered By - stolzem