bash:strings:check_if_a_string_contains_a_substring
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
bash:strings:check_if_a_string_contains_a_substring [2021/01/11 11:17] – created peter | bash:strings:check_if_a_string_contains_a_substring [2021/01/11 11:21] (current) – peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== BASH - Strings - Check if a String contains a Substring ====== | ====== BASH - Strings - Check if a String contains a Substring ====== | ||
+ | |||
+ | ===== Using double brackets with wildcard ===== | ||
<code bash> | <code bash> | ||
Line 14: | Line 16: | ||
A simple comparison operator is used (i.e. **==**), not the regex operator **=~**. | A simple comparison operator is used (i.e. **==**), not the regex operator **=~**. | ||
+ | |||
+ | The comparison can be reversed by just switching to **!=** in the test. | ||
</ | </ | ||
+ | ---- | ||
+ | |||
+ | ===== Regex approach ===== | ||
+ | |||
+ | <code bash> | ||
+ | string=' | ||
+ | |||
+ | if [[ $string =~ " | ||
+ | echo " | ||
+ | fi | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Using a case statement ===== | ||
+ | |||
+ | <code bash> | ||
+ | case " | ||
+ | *foo*) | ||
+ | # Do stuff | ||
+ | ;; | ||
+ | esac | ||
+ | </ |
bash/strings/check_if_a_string_contains_a_substring.1610363855.txt.gz · Last modified: 2021/01/11 11:17 by peter