Regular expression script

Another simple demonstration of conditional testing using regular expressions will be to expose the US and UK spelling of color, being color and colour respectively. We may prompt the user if they want a color or mono output for the script but at the same time cater for both spellings. The line that will do the work in the script is as follows:

if [[ $REPLY =~ colou?r ]] ; then

The regular expression caters to both spellings of color by making the u optional: u?. Furthermore, we can disable case sensitivity allowing for COLOR and color by setting a shell option:

shopt -s nocasematch

This option can be disabled again at the end of the script with the following command:

shopt -u nocasematch  

When we use the variable ...

Get Mastering Linux Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.