There are differences between versions of some of the relatively common utilities that occasionally make it necessary to check which version is installed in a script, but comparing version numbers in a script is quite hard – fortunately if you use Debian there is are a couple of commands that let you do this.

Bash is ultimately intended to be a conformant implementation of the
IEEE POSIX Shell and Tools specification (IEEE Working Group 1003.2).

The Programmable Completion Code, by Ian Macdonald, is now found in
the bash-completion package.
$

Somewhere in there is the version number, but the question is how to extract it.

Looking through the man pages I spotted the following section:

# man dpkg-query
:
:
:-f, --showformat=format
This option is used to specify the format of the output --show
will produce. The format is a string that will be output for
each package listed..
In the format string, "\" introduces escapes:

\n newline
\r carriage return
\t tab

"\" before any other character suppresses any special meaning
of the following character, which is useful for "\" and "$".

Package information can be included by inserting variable ref‐
erences to package fields using the syntax "${field[;width]}".
:
:
:
$

This is followed by a long list of fields that can be used, one of which is the package version.

$ man dpkg
:
:
:--compare-versions ver1 op ver2
Compare version numbers, where op is a binary operator. dpkg
returns success (zero result) if the specified condition is
satisfied, and failure (nonzero result) otherwise. There are
two groups of operators, which differ in how they treat an
empty ver1 or ver2. These treat an empty version as earlier
than any version: lt le eq ne ge gt. These treat an empty ver‐
sion as later than any version: lt-nl le-nl ge-nl gt-nl. These
are provided only for compatibility with control file syntax:< << <= = >= >> >.
:
:
:
$

Great but that does this mean?

Essentially we can use ‘dpkg’ to compare two version numbers and if the condition is true the status code returned by ‘dpkg’ will be zero (indicating success). So we use this command in an ‘if’ statement to compare version numbers.

Note – The arguments do not have to be enclosed in quotes, and the version numbers do not need to be numeric, though they must start with a digit (leading zeros are truncated) and they don’t have to be package versions.