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:

function has-bom { # Test if the file starts with 0xEF, 0xBB, and 0xBF head -c 3 "$1" | grep -P '\xef\xbb\xbf' return $? }

For some reason, head seems to be ignoring the BOM in front of the file. As an example, running this

printf '\xef\xbb\xbf' > file head -c 3 file

won't print anything.