Skip to content
Advertisement

How to get python version in shell script?

Objective: to get python version in shell script

Observation:

[root@srvr0 ~]# python --version
Python 2.7.5

Code:

export python_version=`python --version`
echo "$python_version=$python_version"

Expected:

$python_version=Python 2.7.5

Actual:

$python_version=

Please help me getting python version in shell script.

Advertisement

Answer

the –version writes to stderr, so:

export python_version=$(python --version 2>&1)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement