BASH contains, non case sensitive

408 Views Asked by At

In BASH how can I tell if the string contains another string, ignoring upper or lower case.

Example:

if [[ $FILE == *.txt* ]]
then
   let FOO=1;
fi

I would like this statement to be true no matter if the value of $FILE is upper, lower or mixed.

1

There are 1 best solutions below

0
On

One way is to covert FILE to lower-case before you test using tr:

lowerFILE="$( tr [A-Z] [a-z] <<<"$FILE" )"

if [[ $lowerFILE == *.txt* ]]
then
  let FOO=1;
fi

Example:

#!/bin/bash

for FILE in this.TxT that.tXt other.TXT; do 
    lowerFILE="$( tr [A-Z] [a-z] <<<"$FILE" )"
    [[ $lowerFILE == *.txt* ]] && echo "FILE: $FILE ($lowerFILE) -- Matches"
done

output:

FILE: this.TxT (this.txt) -- Matches
FILE: that.tXt (that.txt) -- Matches
FILE: other.TXT (other.txt) -- Matches