Skip to content

Instantly share code, notes, and snippets.

View soliton-'s full-sized avatar

Gunter Labes soliton-

  • 17:25 (UTC +01:00)
View GitHub Profile
@soliton-
soliton- / vcompare
Last active July 7, 2026 16:54
version compare for bash
#!/bin/bash
# compare version $1 and $2; returns 0 if version $1 >= $2
# only the initial numeric part of a version component is considered
vcompare() {
local v1=$1 v2=$2 v1a v2a n1 n2 max i
IFS=. read -ra v1a <<< "$v1"; IFS=. read -ra v2a <<< "$v2"
((n1=${#v1a[@]}, n2=${#v2a[@]}, max=(n1 > n2 ? n1 : n2)))
# remove trailing non-digits and leading zeros (to avoid octal)
for ((i=0; i < n1; ++i)) do v1a[i]=${v1a[i]%%[!0123456789]*} v1a[i]=${v1a[i]#"${v1a[i]%%[!0]*}"}; done