I want to check if the user input is a valid IP address/CIDR in bash and I am using regular expressions to do that. So valid CIDR should be 0-32 and for IP from (1-254).(1-255).(1-255).(1-255)/(1-32) So my code currently is:

read -p "Input: " ip_address

if [[ $ip_address =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/[0-9]+$ ]];
    echo "VALID"
    echo "NOT VALID"

But it is too generous and accepts some not valid combinations. So a valid IP address/CIDR combination should be: or and invalid will be because the first octet is higher than 255 or here the CIDR is is higher than 32. Is there another way apart of regex to check for valid IP address/CIDR?


To properly handle validation of an IP address or CIDR, use a library function specifically made for this, such as the cidrvalidate() Perl function in the Net::CIDR module:

$ perl -MNet::CIDR=cidrvalidate -e 'printf("%s\n", cidrvalidate($ARGV[0]) ? "valid" : "invalid")' --

$ perl -MNet::CIDR=cidrvalidate -e 'printf("%s\n", cidrvalidate($ARGV[0]) ? "valid" : "invalid")' --

$ perl -MNet::CIDR=cidrvalidate -e 'printf("%s\n", cidrvalidate($ARGV[0]) ? "valid" : "invalid")' --

See perldoc Net::CIDR for what else this library can do.

The -- are not necessary in the examples above but would be for arbitrary input from the user as otherwise, that input would be taken as an option by perl if it started with -.

The approaches below are variations on your attempt, which does not care about invalid netmasks.

A positive decimal integer between 0 and 255 may be matched by


A positive decimal integer between 0 and 32 may be matched by


Using this:



IFS= read -rp 'Input: ' ipaddr

if [[ $ipaddr =~ ^$n(\.$n){3}/$m$ ]]; then
    printf '"%s" is a valid CIDR\n' "$ipaddr"
    printf '"%s" is not valid\n' "$ipaddr"

The expression


would expand to the full regular expression for a valid CIDR spanning the complete length of the given string.

The other obvious way to do this is to read the numbers in the given string and test whether the first four are in the 0-255 range and whether the fifth is in the range 0-32:


IFS='./' read -rp 'Input: ' a b c d e

for var in "$a" "$b" "$c" "$d" "$e"; do
    case $var in
            printf 'not a valid number: %s\n' "$var"
            exit 1


if [ "$a" -ge 0 ] && [ "$a" -le 255 ] &&
   [ "$b" -ge 0 ] && [ "$b" -le 255 ] &&
   [ "$c" -ge 0 ] && [ "$c" -le 255 ] &&
   [ "$d" -ge 0 ] && [ "$d" -le 255 ] &&
   [ "$e" -ge 0 ] && [ "$e" -le 32  ]
    printf '"%s" is a valid CIDR\n' "$ipaddr"
    printf '"%s" is not valid\n' "$ipaddr"

Here we read five words into five variables. The inputted string is split into words on . and / when read (which means 3/3/3/3.2 would be parsed as valid, but see $ipaddr in the code). Any non-integer data read would trigger the script to exit. We then start testing their values against the valid ranges. If any test fail, the address entered is not valid.

  • thanks a lot for your help Stephane Chazelas and @Kusalananda – Georgе Stoyanov Mar 11 at 9:20

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.