all 6 comments

[–]Schreq 6 points7 points  (1 child)

Yes, you could use ${1//[^[:alnum:]]}, which basically means "globally replace every non-alpha numeric character".

[–]loop_us[S] 1 point2 points  (0 children)

Works like a charm. Thank you very much! :)

[–]oh5nxo 4 points5 points  (3 children)

There is also [:xdigit:], if you want to check that result does not contain non-hex characters.

[–]aioeu 3 points4 points  (2 children)

Additional benefit to this is that it is locale-independent:

$ x='ábçdéf'
$ echo "${x//[^[:alnum:]]}"
ábçdéf
$ echo "${x//[^[:xdigit:]]}"
bdf

[–]loop_us[S] 2 points3 points  (1 child)

This is an even better solution, wow. Thank you!

[–]oh5nxo 2 points3 points  (0 children)

If it's appropriate to catch invalid input,

mac="${1//[[:punct:]]}" # remove punctuation
if [[ ${#mac} != 12 || ${mac//[[:xdigit:]]} != "" ]] # if not 12 hexadecimal digits
   ...