Case insensitive regex in bash

Posted in howto , Linux

If you want to match a regular expression while ignoring the case, shopt is the easiest method is to use.

Shopt toggles the values of variables controlling optional shell behavior. It is a bash built-in command.

regex

#!/bin/bash
var=TeSTvaRiABlE
shopt -s nocasematch

if [[ "$var" =~ TESTVARIABLE ]]; then
  echo "my regex is crazy go nuts"
else
  echo "fail"
fi

my regex is crazy go nuts

You can use shopt set, unset, and print out settable options such as the ability to expand aliases and debugging options.

Another use you might find for shopt is checking to see if you’re in a restricted shell and whether or not you were called from a login shell.

Posted by admica   @   8 November 2010

Related Posts

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

Name

Email

Website

*

Previous Post
«
Next Post
»
Powered by Wordpress   |   Lunated designed by ZenVerse

Valid XHTML 1.0 Transitional