Yarin Yarin - 1 year ago 214
Linux Question

sed command with -i option failing on Mac, but works on Linux

I've successfully used the following

command to search/replace text in Linux:

sed -i 's/old_link/new_link/g' *

However, when I try it on my Mac OS X, I get:

"command c expects \ followed by text"

I thought my Mac runs a normal BASH shell. What's up?


According to @High Performance, this is due to Mac
being of a different (BSD) flavor, so my question would therefore be how do I replicate this command in BSD


Here is an actual example that causes this:

sed -i 's/hello/gbye/g' *

Answer Source

If you use the -i option you need to provide an extension for your backups.

If you have:


The command:

sed -i '.original' 's/old_link/new_link/g' *

create 2 backup files like:


There is no portable way to avoid making backup files because is impossible to find a mix of sed commands that works on all cases:

  • sed -i -e ... - doe not work on OS X as it creates -e backups
  • sed -i'' -e ... - does not work on OS X 10.6 but works on 10.9+
  • sed -i '' -e ... - not working on GNU
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download