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?
Using Regex Operator Another option to determine whether a specified substring occurs within a string is to use the regex operator =~ . When this operator is used, the right string is considered as a regular expression. The period followed by an asterisk .
When comparing strings in Bash you can use the following operators: string1 = string2 and string1 == string2 - The equality operator returns true if the operands are equal. Use the = operator with the test [ command. Use the == operator with the [[ command for pattern matching.
We can use the double equals ( == ) comparison operator in bash, to check if a string starts with another substring. In the above code, if a $name variable starts with ru then the output is “true” otherwise it returns “false”.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With