0

How can I find if a pattern has occurred in a variable in if condition.

Eg:

var1="DEFABCTY"
var2="EFGH"

How can I use if in shell script to find which of the two variables has "ABC" using if statement.

1
  • 3
    This is quite simple and has been asked many times. What have you tried and what are you missing? Commented Oct 13, 2014 at 13:13

1 Answer 1

1

You can use pattern matching and variable indirection:

for var in var1 var2 ; do
    if [[ ${!var} = *ABC* ]] ; then
        echo ABC occurs in $var
    fi
done    
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.