Friday, January 29, 2010

Regular expressions in bash

You can perform regular expression matching on a variable within an extended test command (see the Conditional Constructs part of the bash manual).

e.g.

prompt> name=foobar.blarg;if [[ $name =~ foo ]]; echo yep; else echo nope; fi
yep
prompt> name=foobar.blarg;if [[ $name =~ foo[a-c] ]]; echo yep; else echo nope; fi
yep
prompt> name=foobar.blarg;if [[ $name =~ foo[d-z] ]]; echo yep; else echo nope; fi
nope

No comments:

Post a Comment