Fork me on GitHub

Mac下用sed报错 command c expects followed by text

一条很简单的替换文本的命令:

1
2
3
sed -i 's/www.a.cn/www.b.com/g' common.sh
# 执行时报错command c expects \ followed by text
# 也有可能报错 invalid command code W

但是同样的命令在服务器上是可以正确执行的,看了mac下的man sed,里面-i选项中的解释:

1
2
3
4
5
6
7
8
9
10

Edit files in-place, saving backups with the specified extension.

If a zero-length extension is given, no backup will be saved. It

is not recommended to give a zero-length extension when in-place

editing files, as you risk corruption or partial content in situ-

ations where disk space is exhausted, etc.

大概的意思就是说用-i的选项时mac会进行强制备份,所以你必须给出一个后缀名进行备份,如果你不想进行备份,直接用空字符即可

1
sed -i '' 's/www.a.cn/www.b.com/g' common.sh
------------- 本文结束感谢您的阅读 -------------