Stop bash variable expansion from making your sed regex a mess

Shell Scripting / Bash Tricks

Stop bash variable expansion from making your sed regex a mess

🧩 The Challenge

Trying to use a shell variable inside a sed command usually leads to a headache because forward slashes in file paths break your delimiter. I’ve spent way too much time escaping every single slash by hand like it’s 1995.

💡 The Fix

Use a different delimiter for your sed expression that isn’t a forward slash. You can pick almost any character that doesn’t appear in your data to keep the syntax clean.

sed -i "s|/var/www/html/old-path|/opt/new-path|g" config.conf

⚙️ Why It Works

Because sed allows you to use almost any non-whitespace character as a delimiter after the ‘s’, the engine stops freaking out when it sees a file path. Changing that first character makes the command much easier to read and debug.

🚀 Pro-Tip: Stick to a pipe or an underscore for your custom delimiter, they’re usually the safest bet.

Linux Tips & Tricks | © ngelinux.com | 8/4/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted