Rahul's Blog

Bash Shell Scripting: Regular Expressions (Regex)

July 21, 2020

#!/bin/bash

# with extended test [[ ]] you can use regular expressions

read -p "Validate date: " date
pat="^[0-9]{8}$"

if [[ $date =~ $pat ]]; then
    echo "$date is valid"
else
    echo "$date is invalid"
fi

Written by Rahul Jain         
I write code like I make sandwich