Shell script Bash, Check if string starts and ends with single quotes
bash, shell
Solution
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.
Problem
I need to check if a string starts and ends with a single quote, for example 'My name is Mozart' What I have is this, which doesn't work ``` if [[ $TEXT == '*' ]] ; ``` This does not work either ``` if [[ $TEXT == /'*/' ]] ; ``` But if I change it to ``` if [[ $TEXT == a*a ]] ; ``` it works for a sentence like 'an amazing apa'. So I Believe it has to do with the single quote sign. Any ideas on how I can solve it?