how can I test if a string matches a particular string matches regex with a basic (no bash or anything) posix shell script (in an if statement)?
Asked
Active
Viewed 7,152 times
1 Answers
14
You can use expr
command to evaluate regular expression in a POSIX shell:
s='Abc'
expr $s : '^[[:alpha:]]\+'
3
expr
returns # of matched characters which is 3 in this case.

anubhava
- 761,203
- 64
- 569
- 643
-
4Caveat: for `expr`, a ‘^’ is implicitly prepended, also the regex has to be in the basic form. – ryenus Apr 05 '18 at 04:04