How to test if string matches a regex in POSIX shell? (not bash)
You could use expr
:
if expr "$string" : "My" 1>/dev/null; then
echo "It's there";
fi
This would work with both sh
and bash
.
As a handy function:
exprq() {
local value
test "$2" = ":" && value="$3" || value="$2"
expr "$1" : "$value" 1>/dev/null
}
# Or `exprq "somebody" "body"` if you'd rather ditch the ':'
if exprq "somebody" : "body"; then
echo "once told me"
fi
Quoting from man expr
:
STRING : REGEXP
anchored pattern match of REGEXP in STRING
Using grep for such a simple pattern can be considered wasteful. Avoid that unnecessary fork, by using the Sh built-in Glob-matching engine (NOTE: This does not support regex):
case "$value" in
*XXX*) echo OK ;;
*) echo fail ;;
esac
It is POSIX compliant. Bash have simplified syntax for this:
if [[ "$value" == *XXX* ]]; then :; fi
and even regex:
[[ abcd =~ b.*d ]] && echo ok
The [[ ... ]]
are a bash-ism. You can make your test shell-agnostic by just using grep
with a normal if
:
if echo "$string" | grep -q "My"; then
echo "It's there!"
fi