1

I want to check the following condition, but it should be case insensitive. if [ "SPP" == $1 ] Is there anyway I can do it using regex.

3 Answers 3

2

You can also do the following:

#!/bin/bash
myParam=`echo "$1" | tr 'a-z' 'A-Z'`
if [ "SPP" == "$myParam" ]; then
    echo "Is the same"
else
    echo "It is not the same"
fi

This script will automatically converts user input to uppercase before making any string comparison. By doing so, you will not have to use regex for case insensitive string comparison.

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

1

Better late than never...

If that's ksh93, use the ~(i:...) case-insensitive globbing sub-pattern:

if [[ $1 == *~(i:spp)* ]]; then
  : matched.
fi

For ksh88 (also the ksh clones), use an intermediary variable typeset -u'd to force upper-case:

typeset -u tocheck=$1
if [[ $tocheck == *SPP* ]]; then
  : matched
fi

Comments

0

You can use:

shopt -s nocasematch

For case insensitive matching in BASH.

Alternatively this should also work:

[[ "$1" == [sS][pP][pP] ]]

10 Comments

if [ "[sS][pP][pP]" == "$1" ] then echo $1 else echo "hello" fi On running the above script by passing spp as parameter, I am not getting the desired output ./b.sh spp hello
Did you try: shopt -s nocasematch?
Ok I made a change as quotes are not needed. Try: [ [sS][pP][pP] == "$1" ]
not sure but this is also not working: if [ [sS][pP][pP] == "$1" ] then echo "true" else echo "false" fi
No both conditions are not same. Shell glob pattern works on right hand side only.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.