shell

Replace Single Quotes Within Single Quotes Using sed

Introduces how to replace single quotes within single quotes using sed in Linux command line or Bash. I don't quite understand the logic, but if you want to replace single quotes, don't use <code>\'</code> or similar...

Shou Arisaka
1 min read
Oct 12, 2025

Introduces how to replace single quotes within single quotes using sed in Linux command line or Bash.

$ echo \'aaaa\' | sed 's/'\''/1/g'
1aaaa1

I don’t quite understand the logic, but if you want to replace single quotes, don’t use or similar, use '''. To make it clearer by replacing with a string, it’s [single quote][backslash][single quote][single quote]. You might think you could avoid this hassle by enclosing with double quotes instead of single quotes, but that causes other problems, so it’s better not to.

sedでダブルクォーテーション・シングルクォーテーションの置換・削除を行う | 俺的備忘録 〜なんかいろいろ〜

Share this article

Shou Arisaka Oct 12, 2025

🔗 Copy Links