Last active
June 27, 2024 22:13
-
-
Save fabiolimace/79f0494684bd2902f7366755337e9712 to your computer and use it in GitHub Desktop.
REGEX match for `if` in shells such as Dash (default /bin/sh in Ubuntu)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# using expr | |
expr_match() { | |
local text="${1}"; | |
local rexp="${2}"; | |
# NOTE: expr regex is ancored with '^' | |
expr "${text}" : "${rexp}" > /dev/null; | |
} | |
# using grep | |
grep_match() { | |
local text="${1}"; | |
local rexp="${2}"; | |
echo "${text}" | grep -E -q "${rexp}"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if expr_match "string" "s"; then | |
echo "true" | |
fi; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
BUG in GNU's
expr
:The problem doesn't occur in Busybox's
expr
.