I don't think there should be a `$` before `BRANCH`, and a trailing `/` inside the braces:
```bash
ESCAPED_BRANCH=${BRANCH/\/&/\\\&}
```