Reputation: 2558
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.
Upvotes: 0
Views: 579
Reputation: 4382
the --version writes to stderr, so:
export python_version=$(python --version 2>&1)
Upvotes: 2
Reputation: 111
The command output being in stderr rather than stdout really tripped me up for a while; definitely wasn't expected behavior for me.
Anyways, if anyone comes here trying to do this on Windows, you can do this:
for /f "USEBACKQ tokens=2" %G in (`"python --version 2>&1"`) do set pyVersion=%G
You'll then have a string like '2.7.18' in the pyVersion variable. Remember to expand %G to %%G if using it within a batch script.
Upvotes: 0