Shell script to removes UTF-8 Byte Order Mark (BOM) from text files (when present).
#!/usr/bin/env bash
#
# Remove Byte Order Mark from File
#
# Shell script to removes UTF-8 Byte Order Mark (BOM) from text files, where
# present.
#
# https://github.com/ActiveState/code/tree/master/recipes/Bash/577912_Remove_UTF8_Byte_Order_Mark_BOM_text
#
for x in "$@"; do
if [[ "$(file "$x")" == *UTF-8\ Unicode\ \(with\ BOM\)* ]]; then
echo "Removing UTF-8 BOM for $x"
# The +4 tells it to tail from the 4th byte (skipping BOM)
tail -c +4 "$x" > "/tmp/killbom" || { echo "Failed to tail to /tmp/killbom"; exit 1; }
mv "/tmp/killbom" "$x"
fi
done