Find substring in shell script variable Find substring in shell script variable bash bash

Find substring in shell script variable


What shell? Using bash:

if [[ "$VAR" =~ "UAT" ]]; then    echo "matched"else    echo "didn't match"fi


You can do it this way:

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


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.