Bash Regular Expressions: Reference and Examples

Last updated:

Simplest matching

if [[ "foo" =~ ^f ]]; then
 echo "match"
fi

Matching with character classes and escaping stuff

# matches 123213.zip, foobar123.zip
for filename in "$path"/*; do
  if [[ "$filename" =~ [[:digit:]]+\.zip$ ]]; then
    echo "$filename matches"
  else
    echo "$filename does not match"
  fi 
done 

See also

Dialogue & Discussion