Monday, September 5, 2022

[SOLVED] how to Find a substring in a bash shell script variable

Issue

I have a variable like below.

variable = This script is not found

if [[ "$variable" = ~ "not found" ]];
then
echo "Not Found"
else
echo "Its there"
if

while executing im getting below err,

line 4: syntax error in conditional expression
./test.sh: line 4: syntax error near `found"'
./test.sh: line 4: `if [[ "$variable" = ~ "not found" ]]; '

could anyone point me, What im missing here?


Solution

LIST="some string with a substring you want to match"
SOURCE="substring"

if echo "$LIST" | grep -q "$SOURCE"; then
    echo "matched";
else
    echo "no match";
fi

Good Luck ;)



Answered By - Quentin Perez
Answer Checked By - Mary Flores (WPSolving Volunteer)