A byte order mark (BOM) is the Unicode character U+FEFF placed at the very beginning of a text stream to indicate which Unicode encoding the data uses and, for multi-byte encodings such as UTF-16 and UTF-32, in which byte order the code units are stored. In software interoperability, the BOM is one of the few in-band signals that lets a program identify an encoding without external metadata.
The mark is helpful in some environments and harmful in others. Windows tools have long written a BOM to mark UTF-8 files, while many Unix utilities, compilers, web servers and data parsers treat it as unexpected content. Knowing when to emit, expect or strip a BOM is therefore a practical requirement for anyone exchanging text files across platforms.
How the byte order mark works
The BOM relies on a property of the code point U+FEFF, originally named ZERO WIDTH NO-BREAK SPACE. When it is serialized, its bytes appear in a distinctive pattern for each encoding form. Its byte-swapped counterpart, U+FFFE, is a noncharacter that must never appear in valid text, so a reader that sees FF FE instead of FE FF can conclude with confidence that the data is little-endian.
A decoder inspects the first two to four bytes of the stream, compares them against known signatures and, if one matches, selects the encoding and discards the mark. The table lists the signatures defined by the Unicode Standard.
| Encoding | BOM bytes (hex) | Length |
|---|---|---|
| UTF-8 | EF BB BF | 3 bytes |
| UTF-16 big-endian | FE FF | 2 bytes |
| UTF-16 little-endian | FF FE | 2 bytes |
| UTF-32 big-endian | 00 00 FE FF | 4 bytes |
| UTF-32 little-endian | FF FE 00 00 | 4 bytes |
The overlap between the UTF-16LE and UTF-32LE signatures shows why detection order matters: a decoder must test for the four-byte UTF-32 pattern before accepting the two-byte UTF-16 pattern. Rarely used encodings such as UTF-7 and UTF-EBCDIC also have signatures, but they have little practical relevance today.
BOM in UTF-8 versus UTF-16 and UTF-32
The BOM has a different purpose in each encoding family. In UTF-16 and UTF-32, the code units span several bytes, so a reader genuinely needs to know their order, which is the subject of endianness. In UTF-8, the code units are single bytes, so byte order does not exist and the mark only serves as an encoding signature.
The Unicode Standard permits a UTF-8 BOM but neither requires nor recommends it. The table summarizes how each form treats the mark.
| Encoding form | Main purpose of the BOM | Status |
|---|---|---|
| UTF-8 | Encoding signature only | Permitted, not recommended |
| UTF-16 (unlabeled) | Declares byte order | Expected; big-endian assumed if absent |
| UTF-16BE or UTF-16LE (labeled) | None, order is declared externally | Should not be used |
| UTF-32 (unlabeled) | Declares byte order | Optional |
| UTF-32BE or UTF-32LE (labeled) | None | Should not be used |
The labeled forms create a subtle trap. When a protocol or file header already says UTF-16LE, a leading U+FEFF is interpreted as an ordinary ZERO WIDTH NO-BREAK SPACE character rather than as a mark. Since Unicode 3.2, the role of an in-text zero-width no-break space has moved to U+2060 WORD JOINER, so U+FEFF should appear only at the start of a stream.
Interoperability problems caused by a BOM
A BOM becomes a defect when the receiving program does not expect it and passes the bytes through as data. Because the mark is invisible in most editors, the resulting errors are notoriously hard to diagnose. The following table collects the most common symptoms.
| Symptom | Cause | Fix |
|---|---|---|
| Characters  at the start of a page or file | UTF-8 BOM decoded as Windows-1252 | Save as UTF-8 without BOM |
| Shell script fails with an unknown interpreter | BOM placed before the shebang line | Strip the BOM from the script |
| PHP warns that headers were already sent | BOM output before the opening tag | Remove the BOM from included files |
| JSON parser rejects valid content | Strict parser forbids a leading BOM | Decode with BOM handling, then parse |
| First CSV column header does not match | BOM merged into the first field name | Read with a BOM-aware codec |
| File hashes differ for identical text | One copy carries a BOM | Normalize encoding before comparison |
The garbled sequence in the first row is a classic form of mojibake. Similar issues arise when files are concatenated: each source file keeps its own BOM, so the merged output contains stray U+FEFF characters in the middle of the text, where they are no longer treated as a mark.
How standards and tools handle the mark
Specifications take different positions on the BOM, and interoperable software has to follow the rules of the format it produces. RFC 8259 for JSON states that implementations must not add a BOM to transmitted JSON text but may ignore one when parsing. The WHATWG Encoding Standard gives a BOM priority over the HTTP charset label when browsers decode a document, and the XML 1.0 specification uses the BOM in its encoding autodetection.
Tool support is equally uneven. Programming languages often provide separate codecs for the two behaviors, for example Python with utf-8 and utf-8-sig, where only the latter strips a leading mark on input and writes one on output. Microsoft Excel relies on a UTF-8 BOM to recognize UTF-8 in CSV files opened directly, while Windows PowerShell 5.1 writes a BOM by default and PowerShell 7 does not.
- Detect a BOM by inspecting the first bytes, never by reading the decoded text.
- Strip the mark once at the input boundary and keep internal strings free of it.
- Emit a BOM only when a known consumer, such as a spreadsheet import, requires it.
Choosing whether to write a BOM
The safest default for modern exchange is UTF-8 without a BOM, with the encoding declared by the protocol or format, such as an HTTP Content-Type header or an XML declaration. A BOM is justified when the file travels without any metadata and the recipient is known to rely on the signature.
| Scenario | Recommendation | Reason |
|---|---|---|
| Source code and scripts | No BOM | Compilers and shells may reject it |
| Web pages and APIs | No BOM | Encoding is declared in headers |
| CSV for spreadsheet users | UTF-8 with BOM | Ensures correct character display |
| Unlabeled UTF-16 files | Include a BOM | Readers need the byte order |
| Configuration files on Linux | No BOM | Parsers may treat it as data |
A non-technical view
A byte order mark is a tiny invisible label at the very start of a text file. It tells the program opening the file which kind of character encoding to expect, a bit like a note on a parcel saying “this side up”.
Some programs read the label and use it correctly. Others do not know what it is and treat it as part of the content, which can show up as strange symbols or break a script. That is why the practical rule is simple: add the label only when the receiver needs it, and leave it out everywhere else.
A concrete example
A small online shop exports its customer list as a UTF-8 CSV file so the accounting team can open it in a spreadsheet. Names such as Müller and Dvořák appear garbled, because the spreadsheet guesses a legacy Windows code page. The developer switches the export to UTF-8 with a BOM, and the names display correctly on the next download.
A week later, a nightly import script written in another language starts failing: the first column, “email”, no longer matches the expected header, since the parser reads the three BOM bytes as part of the field name. The team resolves the conflict by opening the file with a BOM-aware decoder in the script while keeping the mark for spreadsheet users. The outcome is one file that works in both tools, and a short note in the export documentation explaining why the BOM is there.