I'm trying select a part of some output in Bash, but can't figure out where to place the filter and am confused about the output I'm getting.
I'm starting from
declare -a pythons=("python" "python2" "python3")
for p in "${pythons[@]}"
do
echo "$p: $($p --version 2>&1) in $(which $p)" && for i in $(which -a $p); do echo " $i ($($i --version 2>&1))"; done && echo;
done
which has worked for years to produce the output I expect, namely
python: Python 2.7.15 in /usr/local/bin/python
/usr/local/bin/python (Python 2.7.15)
/usr/bin/python (Python 2.7.10)
python2: Python 2.7.15 in /usr/local/bin/python2
/usr/local/bin/python2 (Python 2.7.15)
python3: Python 3.7.0 in /usr/local/bin/python3
/usr/local/bin/python3 (Python 3.7.0)
but now produces
python3: Python 3.7.0 (default, Jun 29 2018, 20:13:13)
[Clang 9.1.0 (clang-902.0.39.2)] in /usr/local/bin/python3
/usr/local/bin/python3 (Python 3.7.0 (default, Jun 29 2018, 20:13:13)
[Clang 9.1.0 (clang-902.0.39.2)])
for the final grouping.
I'd thought that I could simply fix this by adding egrep -o 'Python\s[0-9\\.]+'
right after each python --version
with
for p in "${pythons[@]}"
do
echo "$p: $($p --version | egrep -o 'Python\s[0-9\\.]+' 2>&1) in $(which $p)" && for i in $(which -a $p); do echo " $i ($($i --version | egrep -o 'Python\s[0-9\\.]+' 2>&1))"; done && echo;
done
but this surprises me by pulling the Python 2 versions out onto a separate line before lines where I expect it
Python 2.7.15
python: in /usr/local/bin/python
Python 2.7.15
/usr/local/bin/python ()
Python 2.7.10
/usr/bin/python ()
Python 2.7.15
python2: in /usr/local/bin/python2
Python 2.7.15
/usr/local/bin/python2 ()
python3: Python 3.7.0 in /usr/local/bin/python3
/usr/local/bin/python3 (Python 3.7.0)
I think I've totally lost my bearings about where output is being sent, and need help sorting out where I'm making my — probably very basic — Bash scripting mistake.