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


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"
    echo "Greater than 5.0.0"

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?

  • @123 Nothing happens – Abhimanyu Saharan May 27 '16 at 14:21
  • 1
    There's also a Stack Overflow question with a bunch of different suggestions for comparing version strings. – n.st May 29 '16 at 3:31
  • 1
    Much simpler than using pipes: gcc -dumpversion – Victor Lamoine Jan 4 '17 at 14:18

I don't know if it is beautiful, but it is working for every version format I know.

currentver="$(gcc -dumpversion)"
 if [ "$(printf '%s\n' "$requiredver" "$currentver" | sort -V | head -n1)" = "$requiredver" ]; then 
        echo "Greater than or equal to 5.0.0"
        echo "Less than 5.0.0"

(Note: better version by the user 'wildcard': https://unix.stackexchange.com/users/135943/wildcard , removed additional condition)

  • or you could use sort's own checking facilities if printf '%s\n%s\n' "$(gcc --version | head -n1 | awk '{print $NF}')" 5.0.0 | sort -cV; then echo 'less'; else echo 'more'; fi - admittedly less readable – iruvar May 27 '16 at 15:23
  • 2
    At first I thought this was awful, and then I realized the beauty of shell scripting is precisely in abusing tools like this. +1 – Xiong Chiamiov May 27 '16 at 23:52
  • 2
    This breaks if there are '%' signs in the print statement. Better replace printf "$requiredver\n$currentver" with printf '%s\n' "$requiredver" "$currentver". – phk Nov 5 '16 at 10:50
  • 1
    -V is a GNU extension of sort(1) thus this solution is non-portable. – stefanct Sep 4 '17 at 0:46
  • 1
    @LucianoAndressMartini, see what you think of my edit. – Wildcard Jan 22 '18 at 23:28

Here I give a solution for comparing Unix Kernel versions. And it should work for others such as gcc. I only care for the first 2 version number but you can add another layer of logic. It is one liner and I wrote it in multiple line for understanding.

check_linux_version() {
    version_good=$(uname -r | awk 'BEGIN{ FS="."}; 
    { if ($1 < 4) { print "N"; } 
      else if ($1 == 4) { 
          if ($2 < 4) { print "N"; } 
          else { print "Y"; } 
      else { print "Y"; }

    #if [ "$current" \< "$expected" ]; then
    if [ "$version_good" = "N" ]; then
        current=$(uname -r)
        echo current linux version too low
        echo current Linux: $current
        echo required 4.4 minimum
        return 1

You can modify this and use it for gcc version checking.


We used to do a lot of version checking in a GNU makefile. We shelled out through the makefile facilities. We had to detect old Binutils and buggy compilers and workaround them on the fly.

The pattern we used was:

#!/usr/bin/env bash

CC=$(command -v gcc)
GREP=$(command -v grep)

# Fixup CC and GREP as needed. It may be needed on AIX, BSDs, and Solaris
if [[ -f "/usr/gnu/bin/grep" ]]; then
elif [[ -f "/usr/linux/bin/grep" ]]; then
elif [[ -f "/usr/xpg4/bin/grep" ]]; then

# Check compiler for GCC 4.8 or later
GCC48_OR_LATER=$("$CXX" -v 2>&1 | "$GREP" -i -c -E "gcc version (4\.[8-9]|[5-9]\.)")
if [[ "$GCC48_OR_LATER" -ne 0 ]];

# Check assembler for GAS 2.19 or later
GAS219_OR_LATER=$("$CXX" -xc -c /dev/null -Wa,-v -o/dev/null 2>&1 | "$GREP" -c -E "GNU assembler version (2\.19|2\.[2-9]|[3-9])")
if [[ "$GAS219_OR_LATER" -ne 0 ]];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.