2

I know following command can show version of python

$ python --version

Result:

Python 3.7.4

I want to extract 3.7.4 only.

So, I attempt to following command using awk

$ python --version | awk '{print $2}'

But result still same as command python --version :

Python 3.7.4

I also try to specify delimiter space:

$ python --version | awk -F' ' '{print $2}'

Result still same as python --version :

Python 3.7.4

How do I do?

Cyrus
  • 77,979
  • 13
  • 71
  • 125
curlywei
  • 566
  • 4
  • 15

2 Answers2

3
 python -c 'import platform; print(platform.python_version())'

Output (e.g.):

2.7.1
Cyrus
  • 77,979
  • 13
  • 71
  • 125
2

the reason is that python prints the output on stderr rather than stdout. Solution e.g.:

python --version 2>&1 | awk '{print $2}'
user829755
  • 1,390
  • 11
  • 26