Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have to replace ( with some character in my file, and I can't do it. \( is used for grouping in sed and when I used \\( with sed, it treated it as \( character not as just (. It seems like a tricky case to me.

share|improve this question
Suggestion: if you just want to substitute a character for another, use tr(anslate): echo 'Hello (world)' | tr '()' '[]'; it outputs Hello [world]. – Lekensteyn Feb 8 '13 at 18:42

If you use sed without -r (extended regular expressions) it will work as it uses \(\) rather than () for grouping:

sed -e 's/(/X/g'

For example

$ echo "foo (bar) (baz)" | sed -e 's/(/X/g'
foo Xbar) Xbaz)

If you want to use -r you can put ( inside [], e.g.

sed -re 's/[(]/X/g'

As Lekensteyn remarks using tr might be more appropriate as you are merely replacing and you do not need the full power of regular expressions.

share|improve this answer
Square brackets = Mind blown. Thanks – JZeolla Feb 8 '13 at 18:59
TL;DR: Don't escape it. – qwertzguy May 5 '15 at 23:46
I used this to replace php's deprecated "split" with "explode" function, making sure "split" is preceded by a space and followed by a "(". grep -lRZ " split(" . | xargs -0 sed -i -e 's/ split(/ explode(/g' – Buttle Butkus Jun 4 at 2:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.