grep with Strings
In this page:
grep with Strings
grep searches text for lines matching a pattern and is commonly combined with echo or a variable to test whether a string contains a substring. grep -q suppresses output and just sets the exit status, which is handy inside an if. grep -i makes the match case-insensitive.
Note: Use if echo "$text" | grep -q "pattern"; then to test whether a variable contains a substring.
Example: grep with Strings
#!/bin/bash
text="The quick brown fox"
if echo "$text" | grep -q "quick"; then
echo "Found 'quick' in the text"
fi
echo "$text" | grep -o "[a-z]*o[a-z]*"
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: