User Tools

Site Tools


bash:strings:check_if_a_string_contains_a_substring

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
bash:strings:check_if_a_string_contains_a_substring [2021/01/11 11:17] – created peterbash: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.
 </WRAP> </WRAP>
  
 +----
 +
 +===== Regex approach =====
 +
 +<code bash>
 +string='My string';
 +
 +if [[ $string =~ "My" ]]; then
 +   echo "It's there!"
 +fi
 +</code>
 +
 +----
 +
 +===== Using a case statement =====
 +
 +<code bash>
 +case "$string" in 
 +  *foo*)
 +    # Do stuff
 +    ;;
 +esac
 +</code>
bash/strings/check_if_a_string_contains_a_substring.1610363855.txt.gz · Last modified: 2021/01/11 11:17 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki