I am trying to check if a variable matches a regex in POSIX shell. However, this seems more difficult than I thought. I am trying to check if the variable is a valid size string (e.g. 10M or 6G etc)
if echo $var | grep -Eq '^\d+[MG]$';
then
echo "match"
else
echo "no match"
fi
That's what I tried, but for some reason I never get a match, even if the variable contains the correct string? Any idea why?
https://shellcheck.net
, a syntax checker, or installshellcheck
locally. Make usingshellcheck
part of your development process. – waltinator May 10 '21 at 00:23