24

I have a string

$VAR="I-UAT"; 

in my shell script code. I need a conditional statement to check if "UAT" is present in that string.

What command should I use to get either true or false boolean as output? Or is there any other way of checking it?

7 Answers 7

26

What shell? Using bash:

if [[ "$VAR" =~ "UAT" ]]; then
    echo "matched"
else
    echo "didn't match"
fi
Sign up to request clarification or add additional context in comments.

5 Comments

Also, bash only, [[ "$VAR" = *UAT* ]] -- right hand side must be unquoted for pattern matching.
pretty sure that's a bash only feature. Is your /bin/sh a symlink to bash?
voted -1 because this answer is bash-specific while the case-based solutions will work on any POSIX shell.
@adl - I guess I understand the logic there, but clearly the OP was using bash or my answer would not have been accepted. I would argue that you should edit the question so that it reflects the OP's intention, rather than downvote answers that are specific to a certain shell.
@Andrew - I too believe the OP uses bash, but since he did not mention it in the question we cannot be sure this is intentional. Maybe he simply does not know that many different shells have all sort of extensions that you should avoid if you want to write portable scripts. In that case he might later wonder why his script fails in other environments. Since there is an easy solution that is portable, I honestly believe it is best to avoid exotic features.
9

You can do it this way:

case "$VAR" in
  *UAT*)
   # code when var has UAT
  ;;
esac

1 Comment

case "$VAR" in can always be replaced by case $VAR in. Shells do not perform field splitting or pathname expansion on the word that follows case. Similarly, var="$other" is as safe as var=$other.
6

The classic way, if you know ahead of time what string you're looking for, is a case statement:

case "$VAR" in
*UAT*) : OK;;
*)     : Oops;;
esac

You can use an appropriate command in place of the : command. This will work with Bourne and Korn shells too, not just with Bash.

Comments

4
found=`echo $VAR | grep -c UAT`

Then test for $found non-zero.

1 Comment

This will also work. -c, --count Suppress normal output; instead print a count of matching lines for each input file.
1

In bash script you could use

if [ "$VAR" != "${VAR/UAT/}" ]; then
  # UAT present in $VAR
fi

Comments

1

try with grep:

$ echo I\-UAT | grep UAT
$ echo $?
0
$ echo I\-UAT | grep UAX
$ echo $?
1

so testing

if [ $? -ne 0 ]; then
  # not found
else
  # found
fi

Comments

0

I like this a little better than using case/esac (and it'll work with non-bash shells):

#!/bin/sh

full_string="I-UAT"
substring="UAT"

if [ -z "${full_string##*$substring*}" ]; then
        echo "Found substring!"
else
        echo "Substring is MIA!"
fi

If the string returned is zero-length (-z), then the substring was found.

Comments

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.