10

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)?

1 Answers1

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
  • 4
    Caveat: for `expr`, a ‘^’ is implicitly prepended, also the regex has to be in the basic form. – ryenus Apr 05 '18 at 04:04