How to compare two strings in dot separated version format in Bash?

前端 未结 29 1352
慢半拍i
慢半拍i 2020-11-22 06:52

Is there any way to compare such strings on bash, e.g.: 2.4.5 and 2.8 and 2.4.5.1?

29条回答
  •  旧时难觅i
    2020-11-22 07:10

    This is also a pure bash solution, as printf is a bash builtin.

    function ver()
    # Description: use for comparisons of version strings.
    # $1  : a version string of form 1.2.3.4
    # use: (( $(ver 1.2.3.4) >= $(ver 1.2.3.3) )) && echo "yes" || echo "no"
    {
        printf "%02d%02d%02d%02d" ${1//./ }
    }
    

提交回复
热议问题