case "$file" in
*_"$testseq"_*) echo 'True' ;;
*) echo 'False' ;;
case ... esac is one of the simplest ways to perform a pattern match in a portable way. It works as a "switch" statement in other languages (
ksh93 also allows you to do fall-through in various ways). The patterns used are the standard file name globbing patterns.
The issue you are having is due to the fact that
_ is a valid character in a variable name. The shell will thus see
*_$testseq_* as "
*_ followed by the value of the variable
$testseq_ and an
*". The variable
$testseq_ is undefined, so it will be expanded to an empty string, and you end up with
*_*, which obviously matches. You may therefore expect to get
True as long as the filename in
$file contains at least one underscore.
To properly delimit the name of the variable, use
"..." around the expansion:
*_"$testseq"_*. This would use the value of the variable as a string. Would you want to use the value of the variable as a pattern, use
Another quick fix is to include the underscores in the value of
and then just use
*"$testseq"* as the pattern (for a string comparison).