sed 替换方括号[]

用 sed 替换字符串中的方括号[],比如将 a[bc]d 替换成 a_bc_d,需要执行:

echo a[bc]d | sed 's/[][]/_/g'

sed手册 里有这么一段:

Most meta-characters lose their special meaning inside bracket expressions:
‘]’
  ends the bracket expression if it’s not the first list item. So, if you want to make the ‘]’ character a list item, you must put it first.

在方括号表达式中大部分元字符失去了原来的意义:
‘]’
  如果它不是列表中的第一个项,则它表示结束方括号表达式。 所以,如果你想让‘]’字符成为列表项,你必须把它放在第一位。

你可能感兴趣的:(sed 替换方括号[])