I am looking for a simple (preferably one-line) command-line expression that sets its exit code to 0 if some expression matches some regular expression and non-zero if it does not match. Ideally, this would be a command “match” that works like this:
$ match "foo.*" "foobar"; echo $?
0
$ match "foo.*" "f00bar"; echo $?
1
A perl command line program (perl -e <…>) would be OK. It does not matter if the command outputs the match to stdout or stderr, all I am interested in is the binary information “match/no match” as an exit code.
In bash, use
=~inside of double brackets.You can even use parentheses to pull out matching groups.