Shell script Bash, Check if string starts and ends with single quotes

I am writing the complete bash script so you won't have any confusion:

#! /bin/bash
text1="'helo there"
if [[ $text1 =~ ^\'.*\'$ ]]; then
        echo "text1 match"
else
        echo "text1 not match"

fi

text2="'hello babe'"
if [[ $text2 =~ ^\'.*\'$ ]]; then
        echo "text2 match"
else
        echo "text2 not match"
fi

Save the above script as matchCode.sh

Now run it as: ./matchCode

output:

text1 not match
text2 match

Ask if you have any confusion.


With a regex:

if [[ $TEXT =~ ^\'.*\'$ ]]

With globbing:

if [[ $TEXT == \'*\' ]]

Tags:

Shell

Bash