How To Use Regular Expression – Regex In Bash Linux?

Linux bash provides a lot of commands and features for Regular Expressions or regex. grep , expr , sed and awk  are some of them. Bash also have =~ operator which is named as RE-match  operator. In this tutorial we will look =~  operator and use cases. More information about regex command cna be found in the following tutorials.

Syntax

Syntax of the bash rematch is very easy we just provide the string and then put the operator and the last one is the regular expression we want to match. We also surround the expression with double brackets like below. [[ STRING =~ REGEX]]

[[ STRING =~ REGEX]]

Match Digits

In daily bash shell usage we may need to match digits or numbers. We can use bash regex operator. We will state numbers with [0-9] like below. But keep in mind that bash regex can be fairly complicated in some cases. In this example we will simple match given line for digits digit=”ismail poftut 12345″ if [[ $digit =~ [0-9] ]]; then    echo “$digit is a digit” else    echo “oops” fi

digit=”ismail poftut 12345″
if [[ $digit =~ [-9] ]]; then    
echo “$digit is a digit”
else    
echo “oops”
fi
Match Digits

Specify Start Of Line

In previous example we have matched digits in the whole line. This is not case some times. We may need to match from start of the line with digits of other character type. We can use ^ to specify start of the line. In this example we will match line which starts with 123 . As we can see it didn’t match.

digit=”ismail poftut 12345″ if [[ $digit =~ ^123 ]]; then    echo “$digit is a digit” else    echo “oops” fi

digit=”ismail poftut 12345″
if [[ $digit =~ ^123 ]]; then    
echo “$digit is a digit”
else    
echo “oops”
fi
Specify Start Of Line

Specify Start Of Line

Specify End Of Line

We can also specify the end on line. We will use $  to specify end of line. We will match line which ends with any digit. digit=”ismail poftut 12345″ if [[ $digit =~ [0-9]$ ]]; then    echo “$digit is a digit” else    echo “oops” fi

digit=”ismail poftut 12345″
if [[ $digit =~ [-9]$ ]];
then    
echo “$digit is a digit”
else    
echo “oops”
fi

Match Email

Digit patterns are easy to express but how can we express email regex in bash. We can use following regex pattern for emails generally. [A-Za-z0-9._%+-]+<b>@</b>[A-Za-z0-9.-]+

[A-Za-z0-9._%+-]+<b>@</b>[A-Za-z0-9.-]+

We will ommit suffixes like com , net  , gov  etc. because there is a lot of possibilities. As we know @ is sitting between username and domain name.

email=$1

if [[ “$email” =~ “^[A-Za-z0-9._%+-]+<b>@</b>[A-Za-z0-9.-]+<b>\.</b>[A-Za-z]{2,4}$” ]]

then    

echo “This email address looks fine: $email”

else    

echo “This email address is flawed: $email”

fi

email=$1
if [[ “$email” =~ “^[A-Za-z0-9._%+-]+<b>@</b>[A-Za-z0-9.-]+<b>\.</b>[A-Za-z]{2,4}$” ]]
then    
echo “This email address looks fine: $email”
else    
echo “This email address is flawed: $email”
fi
Match Email

Match Email

Match IP Address

IP address is another type of important data type which is used in bash and scripting. We can match IP addresses by using bash regex. We will use following regex pattern which is the same with tools like grep and others.

[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}

[-9]{1,3}\.[-9]{1,3}\.[-9]{1,3}\.[-9]{1,3}

ip=$1

if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]];

then    echo “Looks like an IPv4 IP address”

elif [[ $ip =~ ^[A-Fa-f0-9:]+$ ]]; then    

echo “Could be an IPv6 IP address” else    echo “oops”

fi

ip=$1
if [[ $ip =~ ^[-9]{1,3}\.[-9]{1,3}\.[-9]{1,3}\.[-9]{1,3}$ ]]; then    
echo “Looks like an IPv4 IP address”
elif [[ $ip =~ ^[A-Fa-f0-9:]+$ ]]; then    
echo “Could be an IPv6 IP address” else    echo “oops”
fi
Match IP Address

Match IP Address

Comments