String Comparison
In this page:
String Comparison
Inside [[ ]], strings are compared with == (equal), != (not equal), and lexicographically with </>. [[ ]] also supports pattern matching with == against a glob pattern on the right side. String comparison is distinct from the numeric -eq/-lt operators covered earlier.
Warning: Comparing strings with numeric operators like -eq gives wrong or error results — always use ==/!= for strings.
Example: String Comparison
#!/bin/bash
a="apple"
b="banana"
if [[ "$a" == "$b" ]]; then
echo "Equal"
else
echo "Not equal"
fi
if [[ "$a" == a* ]]; then
echo "$a starts with 'a'"
fi
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: