Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if a number has xx.xx.xx format

I am working in linux bash.
I am doing some stuff where I get a version number.
This version number usually takes the following format xx.x.xx or xx.x.x (For example: 10.0.10 or 25.1.7).
Let say I have a var named "version" which represents this number.

How can I check, in linux bash, if this var matches de criterium (xx.x.xx or xx.x.x) ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use =~ regex operator:

VERSION=10.0.25

if [[ "${VERSION}" =~ ^[0-9]+\.[0-9]\.[0-9]+$ ]]; then
    echo ok
    # do something
else
    echo ko
    # do something else
fi
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading