Comparing python -V output for Python version checking in bash
Question: I am creating an install script where I would like to compare the version of
installed default Python with the version I need to have running. Currently
here is my code:

    
    
    #!/bin/bash
    PYTHON="$(python -V)"
    if [[ "$PYTHON = 'Python 2.7.6' ]]
    then echo "Python is installed."
    else echo "Python is not installed."
    fi
    

The response I keep getting is that Python is not installed, but that is the
output when I type in the command python -V.

Any help is greatly appreciated. Thank you in advance.


Answer: It seems that when you run `python -V`, it prints the version to _stderr_ ,
not to stdout. So, changing your attempt to capture the output into a variable
to this:

    
    
    PYTHON=$(python -V 2>&1)
    

should do the trick. Another alternative, which tends to include additional
information about build dates, compilers, etc. would be:

    
    
    python -c 'import sys; print sys.version'
    

or, as suggested by @chepner:

    
    
    python -c 'import sys; print sys.version_info'
    

Both of these would require a little additional parsing to get the specific
information you want/need, though.

