How to compare a program's version in a shell script?
36
votes
12
answers
56155
views
Suppose I want to compare
gcc
version to see whether the system has the minimum version installed or not.
To check the gcc
version, I executed the following
gcc --version | head -n1 | cut -d" " -f4
The output was
4.8.5
So, I wrote a simple if
statement to check this version against some other value
if [ "$(gcc --version | head -n1 | cut -d" " -f4)" -lt 5.0.0 ]; then
echo "Less than 5.0.0"
else
echo "Greater than 5.0.0"
fi
But it throws an error:
[: integer expression expected: 4.8.5
I understood my mistake that I was using strings to compare and the -lt
requires integer. So, is there any other way to compare the versions?
Asked by Abhimanyu Saharan
(911 rep)
May 27, 2016, 02:01 PM
Last activity: Aug 5, 2025, 10:58 AM
Last activity: Aug 5, 2025, 10:58 AM