Bash: Regexp for MAC Address Validation

MAC Address aka HWAddress is a unique identifier assigned to network interfaces.

The address can be accessed using ifconfig utility. It is a 12 digit hexadecimal number, spearated by 5 colons, with 2 digits in pair. The following regexp is the best validator I found.

"^([0-9A-F]{2}:){5}[0-9A-F]{2}$"

Explaination

Example

#!/bin/bash

MAC_ADDR='94:de:80:2b:81:a9'
# capitalize it for faster regexp match
MAC_ADDR=${MAC_ADDR^^}

if [ `echo $MAC_ADDR | egrep "^([0-9A-F]{2}:){5}[0-9A-F]{2}$"` ]
then
    echo "Valid"
else
    echo "Invalid"
fi

Output

Valid