title: How to detect if a file has a UTF-8 BOM in Bash?
body: <p>I'm trying to write a script that will automatically remove UTF-8 BOMs from a file. I'm having trouble detecting whether the file has one in the first place or not. Here is my code:</p>
  
  <pre><code>function has-bom {
      # Test if the file starts with 0xEF, 0xBB, and 0xBF
      head -c 3 "$1" | grep -P '\xef\xbb\xbf'
      return $?
  }
  </code></pre>
  
  <p>For some reason, <code>head</code> seems to be ignoring the BOM in front of the file. As an example, running this</p>
  
  <pre><code>printf '\xef\xbb\xbf' &gt; file
  head -c 3 file
  </code></pre>
  
  <p>won't print anything.</p>
  
  <p>I tried looking for an option in <code>head --help</code> that would let me work around this, but no luck. Is there anything I can do to make this work?</p>