Skip to main content
Better explain why this answer. Get to answer faster.
Source Link
ctwardy
  • 101
  • 3

If the program is Python

The question is "How to compare a program's version in a shell script?" It givesnot gcc as an example. Somespecific though many answers are gcc specific. And* Even the excellent general ones withanswers using cut -d" " stillwill fail for older Python because prior to v3.4, python --version wrote to stderr instead of stdout. (!!)

Here is a short bash command to check that your Python is at least v3.8 (or whatever you use), that uses Python to avoid both stderr issues and the gyrations involved with *sh string comparison.

python -c "import sys; sys.version_info < (3,8) and sys.exit(1)" \
    && echo "At least v3.8!" || echo "Old version. Do not use."

Note that the version is a Python tuple like (3, 8, 18, "final", 0) so if you needed at least v3.8.13, you would use (3, 8, 13).

My use case: stop pip if Python is too old, before I accidentally install hundreds of packages for the wrong environment.

$ python -V \
>   && python -c "import sys; sys.version_info < (3,8) and sys.exit(1)" \
>   && echo "Python version OK" \
>   && echo "UPDATING PIP..." \
>   && python -m pip install --upgrade pip \
>   && echo "UPDATING MYPACKAGE..." \
>   && python -m pip install .[dev] \
>   || echo "Old Python or other error."

Python 2.7.18
Old Python or other error.

* The question is: "How to compare a program's version in a shell script?" It gives gcc as an example but is not limited to gcc.

If the program is Python

The question is "How to compare a program's version in a shell script?" It gives gcc as an example. Some answers are gcc specific. And the excellent general ones with cut -d" " still fail for older Python because prior to v3.4, python --version wrote to stderr instead of stdout. (!!)

Here is a short bash command to check that your Python is at least v3.8 (or whatever you use), that uses Python to avoid both stderr issues and the gyrations involved with *sh string comparison.

python -c "import sys; sys.version_info < (3,8) and sys.exit(1)" \
    && echo "At least v3.8!" || echo "Old version. Do not use."

Note that the version is a Python tuple like (3, 8, 18, "final", 0) so if you needed at least v3.8.13, you would use (3, 8, 13).

My use case: stop pip if Python is too old, before I accidentally install hundreds of packages for the wrong environment.

$ python -V \
>   && python -c "import sys; sys.version_info < (3,8) and sys.exit(1)" \
>   && echo "Python version OK" \
>   && echo "UPDATING PIP..." \
>   && python -m pip install --upgrade pip \
>   && echo "UPDATING MYPACKAGE..." \
>   && python -m pip install .[dev] \
>   || echo "Old Python or other error."

Python 2.7.18
Old Python or other error.

If the program is Python

The question is not gcc specific though many answers are.* Even the excellent general answers using cut -d" " will fail for older Python because prior to v3.4, python --version wrote to stderr instead of stdout. (!!)

Here is a short bash command to check that your Python is at least v3.8 (or whatever you use), that uses Python to avoid both stderr issues and the gyrations involved with *sh string comparison.

python -c "import sys; sys.version_info < (3,8) and sys.exit(1)" \
    && echo "At least v3.8!" || echo "Old version. Do not use."

Note that the version is a Python tuple like (3, 8, 18, "final", 0) so if you needed at least v3.8.13, you would use (3, 8, 13).

My use case: stop pip if Python is too old, before I accidentally install hundreds of packages for the wrong environment.

$ python -V \
>   && python -c "import sys; sys.version_info < (3,8) and sys.exit(1)" \
>   && echo "Python version OK" \
>   && echo "UPDATING PIP..." \
>   && python -m pip install --upgrade pip \
>   && echo "UPDATING MYPACKAGE..." \
>   && python -m pip install .[dev] \
>   || echo "Old Python or other error."

Python 2.7.18
Old Python or other error.

* The question is: "How to compare a program's version in a shell script?" It gives gcc as an example but is not limited to gcc.

Added longer example of my use case. Clarified the tuple comparison.
Source Link
ctwardy
  • 101
  • 3

If the program is Python

The question is "How to compare a program's version in a shell script?" It gives gcc as an example. Some answers are gcc specific. And the excellent general ones with cut -d" " still fail for older Python because prior to v3.4, python --version wrote to stderr instead of stdout. (!!)

Here is a short bash command to check that your Python is at least v3.8 (or whatever you use), that uses Python to avoid both stderr issues and the gyrations involved with *sh string comparison.

python -c "import sys; sys.version_info < (3,8) and sys.exit(1)" \
    && echo "At least v3.8!" || echo "Old version. Do not use."

Note that the version is a Python tuple that beingslike (major3, minor8, patch18, "final", 0) so comparing to v.3if you needed at least v3.8.13, you would beuse (3, 8, 13).

My use case: stop pip if Python is too old, before I accidentally install hundreds of packages for the wrong environment.

$ python -V \
>   && python -c "import sys; sys.version_info < (3,8) and sys.exit(1)" \
>   && echo "Python version OK" \
>   && echo "UPDATING PIP..." \
>   && python -m pip install --upgrade pip \
>   && echo "UPDATING MYPACKAGE..." \
>   && python -m pip install .[dev] \
>   || echo "Old Python or other error."

Python 2.7.18
Old Python or other error.

If the program is Python

The question is "How to compare a program's version in a shell script?" It gives gcc as an example. Some answers are gcc specific. And the excellent general ones with cut -d" " still fail for older Python because prior to v3.4, python --version wrote to stderr instead of stdout. (!!)

Here is a short bash command to check that your Python is at least v3.8 (or whatever you use), that uses Python to avoid both stderr issues and the gyrations involved with *sh string comparison.

python -c "import sys; sys.version_info < (3,8) and sys.exit(1)" \
    && echo "At least v3.8!" || echo "Old version. Do not use."

Note that the version is a Python tuple that beings (major, minor, patch) so comparing to v.3.8.13 would be (3, 8, 13).

If the program is Python

The question is "How to compare a program's version in a shell script?" It gives gcc as an example. Some answers are gcc specific. And the excellent general ones with cut -d" " still fail for older Python because prior to v3.4, python --version wrote to stderr instead of stdout. (!!)

Here is a short bash command to check that your Python is at least v3.8 (or whatever you use), that uses Python to avoid both stderr issues and the gyrations involved with *sh string comparison.

python -c "import sys; sys.version_info < (3,8) and sys.exit(1)" \
    && echo "At least v3.8!" || echo "Old version. Do not use."

Note that the version is a Python tuple like (3, 8, 18, "final", 0) so if you needed at least v3.8.13, you would use (3, 8, 13).

My use case: stop pip if Python is too old, before I accidentally install hundreds of packages for the wrong environment.

$ python -V \
>   && python -c "import sys; sys.version_info < (3,8) and sys.exit(1)" \
>   && echo "Python version OK" \
>   && echo "UPDATING PIP..." \
>   && python -m pip install --upgrade pip \
>   && echo "UPDATING MYPACKAGE..." \
>   && python -m pip install .[dev] \
>   || echo "Old Python or other error."

Python 2.7.18
Old Python or other error.
Source Link
ctwardy
  • 101
  • 3

If the program is Python

The question is "How to compare a program's version in a shell script?" It gives gcc as an example. Some answers are gcc specific. And the excellent general ones with cut -d" " still fail for older Python because prior to v3.4, python --version wrote to stderr instead of stdout. (!!)

Here is a short bash command to check that your Python is at least v3.8 (or whatever you use), that uses Python to avoid both stderr issues and the gyrations involved with *sh string comparison.

python -c "import sys; sys.version_info < (3,8) and sys.exit(1)" \
    && echo "At least v3.8!" || echo "Old version. Do not use."

Note that the version is a Python tuple that beings (major, minor, patch) so comparing to v.3.8.13 would be (3, 8, 13).