Why does sed behave differently depending upon whether it's run from the command line or a shell script? Here's a basic example:
$ cat test.txt
$ sed -e 's/^b\(\w*\)$/q\1/g' test.txt # works as intended
$ cat test.sh # The exact same command
sed -e 's/^b\(\w*\)$/q\1/g' test.txt
$ bash test.sh
gsed (GNU sed) 4.2.2
The issue was simply that I had a long-forgotten bash alias changing
gsed — the GNU version as installed by Homebrew. That explains why
sed --version reported itself as
gsed at the command line. I had checked
which sed from both the script and the prompt, but I didn't think about
type and bash aliases.
$ type sed sed is aliased to `gsed'