I’m trying to match a version number from a string in the format 4.6, or 2.8. I have the following which I will eventually use in a function in my .bashrc file to find the OS version:
function test () {
string="abc ABC12 123 3.4 def";
echo `expr match "$string" '[0-9][.][0-9]'`
}
However, this doesn’t match the 3.4 in the string. Can anyone point me in the right direction here?
Thanks.
First, you can drop the
echo–exprprints its result to stdout in any case.Second, your regex needs brackets (otherwise it prints the number of characters matched, not the match itself), and it needs to begin with
.*.From the
info exprpage: