This is about Bash development and coding of portable Bash scripts that use RegEx.
Using Bash RegEx, on a Mac, I can do this:
coconut-mac$ a='bananacoconutman'; [[ "$a" =~ banana(.*?)man ]] && echo FOUND ${BASH_REMATCH[1]}
FOUND coconut
Nice. Useful in many places. Like.
When I try doing this, it fails:
coconut-mac$ a='<title>coconut</title>'; [[ "$a" =~ \<title\>(.*?)\</title\> ]] && echo FOUND ${BASH_REMATCH[1]}
The exact same command runs perfectly on the penguin:
coconut-linux$ a='<title>coconut</title>'; [[ "$a" =~ \<title\>(.*?)\</title\> ]] && echo FOUND ${BASH_REMATCH[1]}
FOUND coconut
- Why?
- How to fix it to make the script portable?
EDIT: On the Mac:
OS X version: 10.8.2
Bash version: 4.2.37(2)-release
on Ubuntu 12.04 LTS:
Linux kernel version: 3.2.0-29-generic-pae
Linux version: Ubuntu 12.04.1 LTS
Bash version: 4.2.24(1)-release
Best Answer
On my Mac,
info bash / =~ RET
says:man 3 regex
says:I don't see any analogous documentation in GNU regex's
man 3 regex
orinfo regex
.If I remove the
?
from your(.*?)
and do the following, it works on both OSes: