Today I’m going to share a Linux tip that has changed lives.
OK, maybe that’s an overstatement, but every time I’ve shown it to someone, they say “Really!?!? OMG that’s so much easier.” Technically, anyone reading docs could find it, because it’s not secret or undocumented. However, there’s such powerful tradition in Unix/Linux circles that people grow up learning painful ways of doing things. Even this week, I talked with a guy who’s been a Unix/Linux sysadmin since before Linux existed and he didn’t know this trick.
This is applicable to Linux, BSD, and older proprietary Unices. I was shown this on SunOS decades ago, so it goes back to the origins of Unix. It also applies to working in vi/vim, perl, and nearly anywhere regular expressions are used.
The Problem: Backslashitis
I’ll give you an example involving file names, but again, this trick is universal for regular expressions.
Let’s imagine you have a path like this in a shell script:
/some/path/that/is/long
You want to change it to:
/some/path/new/path
How would you do it? A regular expression of course. Let’s use sed. I’m sure you’ve seen things like this:
sed 's/\/that\/is\/long/\/new\/path/'
That works, but it’s confusing to read, isn’t it? Let’s put the actual delimiters in red:
sed 's/\/that\/is\/long/\/new\/path/'
Unfortunately, because you’re using / as your delimiter, you have to escape it wherever you use it otherwise with backslashes, which leads to backslashitis.
The Cure
Did you know you use any character as a delimiter in a regular expression? Try this:
sed 's#/that/is/long#/new/path#'
Whoa! That’s sure a lot easier to read, isn’t it? By replacing the red slashes above with octothorps, I no longer need to escape slashes.
Of course, if your string is full of octothorps, use something else. You can even use spaces:
sed 's /that/is/long /new/path '
As long as the delimiter is the same in all three places, any symbol will work.
You can do this with any regular expression. I’ve seen people (and have myself) struggle with getting the right backslashes in the right place, count them, and agonize over complex regular expressions because visually they’re hard to parse. With this trick, everything becomes so much simpler.
Enjoy!
Related Posts:
Five Times When Updating Your OS Would Have Saved You From Being Hacked
Need a Laugh? Read the Linux Kernel List's Foam-Mouthed Responses to Russian Programmers Banned from...
Nontechnical Nonsense: Rust Stirs Up a Storm of Drama in the Linux Kernel: Ted T'so Shouting, Mainta...
RedHat Prepares to Give Bootloaders the Boot
Irritation Removed: You Do Everything As Root And Hate Manually Removing Sudo From Copy-Paste Comman...
Has the Itanic Finally Hit Its Last Iceberg? It's Been Removed From the Linux Kernel After a Little...

Raindog308 is a longtime LowEndTalk community administrator, technical writer, and self-described techno polymath. With deep roots in the *nix world, he has a passion for systems both modern and vintage, ranging from Unix, Perl, Python, and Golang to shell scripting and mainframe-era operating systems like MVS. He’s equally comfortable with relational database systems, having spent years working with Oracle, PostgreSQL, and MySQL.
As an avid user of LowEndBox providers, Raindog runs an empire of LEBs, from tiny boxes for VPNs, to mid-sized instances for application hosting, and heavyweight servers for data storage and complex databases. He brings both technical rigor and real-world experience to every piece he writes.
Beyond the command line, Raindog is a lover of German Shepherds, high-quality knives, target shooting, theology, tabletop RPGs, and hiking in deep, quiet forests.
His goal with every article is to help users, from beginners to seasoned sysadmins, get more value, performance, and enjoyment out of their infrastructure.
You can find him daily in the forums at LowEndTalk under the handle @raindog308.
Where is the Thanks button when you need one!