Linux Shell Script – String Comparison with wildcards

When using == or != in bash you can write:

if [[ $t1 == *"$t2"* ]]; then
    echo "$t1 and $t2 are equal"
fi

Note that the asterisks go on the outside of the quotes and that the wildcard pattern must be on the right.

For /bin/sh, the = operator is for equality only, not pattern matching. You can use case for pattern matching though:

case "$t1" in
    *"$t2"*) echo t1 contains t2 ;;
    *) echo t1 does not contain t2 ;;
esac

If you’re specifically targeting Linux, I would assume the presence of /bin/bash.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)