Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

71%
+3 −0
Q&A Can I enter raw strings in fish to avoid escaping regexes for sed?

What do you want to find though? Is the + a quantifier, meaning you are looking for one or more b? Or are you looking for the literal string b+? If the latter, you don't need to escape at all and i...

posted 11mo ago by terdon‭

Answer
#1: Initial revision by user avatar terdon‭ · 2023-06-21T19:12:14Z (11 months ago)
What do you want to find though? Is the `+` a quantifier, meaning you are looking for one or more `b`? Or are you looking for the literal string `b+`? If the latter, you don't need to escape at all and if the former, you can just use `sed -E 's/b+/X/'` directly. 

The reason you need to escape in single quoted strings has nothing to do with the shell and is all about the regular expression language used. [By default](https://www.gnu.org/software/sed/manual/html_node/BRE-vs-ERE.html), `sed` will use BRE (basic regular expressions) and in this regex flavor, `+` is just a literal `+` sign and you need `\+` for "one or more". However, most `sed` implementations, including GNU `sed`, the default on Linux, have an `-E` switch which enables ERE (extended regular expressions) and in this regex flavor, `+` has a special meaning and so only needs to be escaped as `\+` if you want to search for a literal `+`. 

All this to say that there won't be a magic bullet here because sometimes you will want to escape special characters and other times you'll want to use them in all their special glory. So if you're coming from more powerful regex flavors such as ERE or PCRE (versions of which are the default in perl and python and many other places), you can just use `-E` to make the regex behave as you likely expect it to:

```
$ echo "foo" | sed -E 's/o+/A/'
fa
$ echo abcdefg | sed -E 's/[b-f]+/A/'
aAg
$ echo foooooo | sed -E 's/o{3,5}/A/'
fAo
```