eg. txtfile is just [

sed -i 's/[/linux/' txtfile (this gives an error unbalanced brackets ([])) after running the command I wanted to replace [ to linux

I tried \\ at the front, but I don't think it is correct and searched a while couldn't find anything useful. Thanks

  • 4
    Please don't use -i with sed unless you know your sed expression actually works.
    – Kusalananda
    May 8 '20 at 6:09
  • @Kusalananda Thank you! Noted!
    – olo
    May 8 '20 at 10:14

All you need is escape the [ using \:

sed -i 's/\[/linux/' txtfile

For completeness, another alternative is to put that [ inside a [...] bracket expression:

sed -i 's/[[]/linux/' txtfile
  • Many thanks for the answer!
    – olo
    May 8 '20 at 10:13

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