The easiest way I found for this is
#!/usr/bin/env bash
#Add BOM to the new file
printf '\xEF\xBB\xBF' > with_bom.txt
# Append the content of the source file to the new file
cat source_file.txt >> with_bom.txt
I know it uses an external program (cat)… but it will do the job easily in bash
Tested on osx but should work on linux as well
NOTE that it assumes that the file doesn’t already have BOM (!)