boop
boop

Reputation: 7788

Compare string with wildcard - ignoring case

What I basically want to do is

if [[ $str == A* ]]; then 
# ...

But I want to ignore the case so both examples should be matched:

str=Abc
str=abc

Upvotes: 1

Views: 525

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80931

You have a few options.

Match both manually:

if [[ $str == [aA]* ]]; then

Use nocasematch:

shopt -s nocasematch
if [[ $str == A* ]]; then

As Tom Fenech points out with bash 4+ (I believe) you can also use the new case modifying Parameter Expansions:

# For lower-casing
if [[ ${str,,} = a* ]]; then

# For upper-casing
if [[ ${str^^} = A* ]]; then

Upvotes: 5

Related Questions