title: How to correctly detect and replace apostrophe (') with sed?
body: <p>I'm having a directory with many files having special characters and spaces. I want to perform an operation with all these files so I'm trying to store all filenames in a <code>list.txt</code> and then run the command with this list.</p>
  
  <p>The special characters in my list are <code>&amp; []'</code>.</p>
  
  <p>So basically I want to use <code>sed</code> to replace each occurence with <code>\</code> + the character in question.</p>
  
  <p>E.g. : <code>filename .txt</code> => <code>filename\ .txt</code> etc... </p>
  
  <p>The thing is I have trouble handling apostrophes.</p>
  
  <p>Here is my command as of now :</p>
  
  <pre><code> ls | sed 's/\ /\\ /g' | sed 's/\&amp;/\\&amp;/g' | sed "s/\'/\\'/g" | sed 's/\[/\\[/g' | sed 's/\]/\\]/g'
  </code></pre>
  
  <p>At first I had issues with, I believe, the apostrophes in the string command in conflict with the apostrophes surrounding the string. So I used double quotes instead, but it still doesn't work.</p>
  
  <p>I've tried all these and nothing worked :</p>
  
  <p><code>sed "s/\'/\\'/g"</code> (escaping the apostrophe)</p>
  
  <p><code>sed "s/'/\'/g"</code> (escaping nothing) </p>
  
  <p><code>sed "s/'/\\'/g"</code> (escaping the backslash)</p>
  
  <p><code>sed 's/"'"/\"'"/g'</code> (double quoting single quote)</p>
  
  <p>As a disclaimer, I must say, I'm completely new to <code>sed</code>. I just run my first <code>sed</code> command today, so maybe I'm doing something wrong I didn't realize.</p>
  
  <p>PS : I've seen those thread, but no answer worked for me :</p>
  
  <p><a href="https://unix.stackexchange.com/questions/157076/how-to-remove-the-apostrophe-and-delete-the-space">https://unix.stackexchange.com/questions/157076/how-to-remove-the-apostrophe-and-delete-the-space</a></p>
  
  <p><a href="https://stackoverflow.com/questions/18119433/how-to-replace-to-apostrophe-inside-a-file-using-sed">How to replace to apostrophe &#39; inside a file using SED</a></p>