Download Caxton for Free · 7-day free trial of Caxton Pro, no credit card required · 8 MB · macOS 13.0+
The short answer
To remove a byte order mark from a UTF-8 file on a Mac, run LC_ALL=C sed $'1s/^\xEF\xBB\xBF//' customers.csv > customers-clean.csv in Terminal. sed is already installed, the command deletes the bytes EF BB BF only when the file starts with them, and head -c 3 customers.csv | xxd shows whether it does. In Caxton, a native Mac editor for text and data files, the Inspector's BOM row reads Present or None, Text ▸ Byte Order Mark ▸ Remove takes the mark off, and ⌘S saves the file without it.
Why does a UTF-8 BOM break an import?
A byte order mark, or BOM, is the character U+FEFF placed at the very start of a file. In UTF-8 it is three bytes, EF BB BF, and it carries no text. The Unicode Consortium's FAQ gives its role in UTF-8 in a few words, "An initial BOM is only used as a signature," and then adds the warning this guide is about: "Note that some recipients of UTF-8 encoded data do not expect a BOM." A program that does not expect the mark reads the three bytes as the beginning of the text. The damage appears wherever the first characters of a file have to be exact: a column name, the first line of a script, the opening brace of a JSON file. Convert a file to UTF-8 explains what the mark is for and which programs ask for one. This guide is for the file that has a mark and should not.
The examples use customers.csv, a small UTF-8 export built for this guide with a mark in front and Windows line endings.
id,name,city
1001,Zoë Hansen,Århus
1002,José Dupont,Lyon
1003,Rossi,Milano
The first column name stops matching
Cut the first field out of the header line and print its bytes.
head -n 1 customers.csv | cut -d, -f1 | xxd
00000000: efbb bf69 640a ...id.
The first field of the header is five bytes long: the three bytes of the mark, then the letters i and d. To a program that compares names, that column is not called id. Here is the kind of lookup an import script makes, which records the position of each column under its name.
awk -F, 'NR == 1 { for (i = 1; i <= NF; i++) col[$i] = i; print "id is column " col["id"] ", name is column " col["name"] ", city is column " col["city"] }' customers.csv
id is column , name is column 2, city is column
The name column is found and the id column is not, and no error is printed. The city column is missing for a second, separate reason. The file has Windows line endings, so the last name on the header line still carries a carriage return, and LF vs CRLF line endings covers that problem.
Python reads the mark as part of the first key
Python's csv module shows the same fault by name. On a Mac, python3 comes with Apple's command line developer tools.
python3 -c 'import csv; print(next(csv.DictReader(open("customers.csv", newline="", encoding="utf-8"))))'
{'\ufeffid': '1001', 'name': 'Zoë Hansen', 'city': 'Århus'}
The first key is \ufeffid, and asking that row for "id" ended in KeyError: 'id'. Python's documentation gives the reason. With the utf-8 codec, "any U+FEFF character in the decoded string (even if it’s the first character) is treated as a ZERO WIDTH NO-BREAK SPACE." The same page documents a second codec, utf-8-sig, which "will skip those three bytes if they appear as the first three bytes in the file." With encoding="utf-8-sig" the same command printed the key as id. If the script is yours, that is the smaller fix. If it is not, remove the mark from the file.
A JSON parser rejects the file
data.json holds one small object, saved with a mark in front.
python3 -m json.tool data.json
Unexpected UTF-8 BOM (decode using utf-8-sig): line 1 column 1 (char 0)
That refusal is documented. RFC 8259, the JSON standard, states that "Implementations MUST NOT add a byte order mark (U+FEFF) to the beginning of a networked-transmitted JSON text," and that a parser "MAY ignore the presence of a byte order mark rather than treating it as an error." Python's json documentation is exact about which it chose: "This module’s deserializer raises a ValueError when an initial BOM is present." Other parsers take the lenient path. On macOS 27, the jq in /usr/bin read the same data.json and printed the object.
A script fails on its first line
hello.sh is a two-line shell script saved with a mark in front of its #! line.
./hello.sh
./hello.sh: line 1: #!/bin/sh: No such file or directory
hello
The mark is in that message as well, invisible, just before the #. The first line was not taken as the line that names the interpreter. It was run as a command, which failed, and then the rest of the script ran. The Unicode FAQ names this case: a mark "will interfere with any protocol or file format that expects specific ASCII characters at the beginning," and its example is the #! line of a Unix shell script.
What import tools document about the BOM
Two data platforms that load CSV files describe the mark in their own documentation.
- Snowflake. Its file format has an option named SKIP_BYTE_ORDER_MARK, which is on by default. The documentation describes what happens when it is off: "Snowflake recognizes any BOM in data files, which could result in the BOM either causing an error or being merged into the first column in the table."
- BigQuery. Google's notes on loading CSV files are shorter: "Remove byte order mark (BOM) characters. They might cause unexpected issues."
Not every program trips over the mark. In the tests for this guide, on macOS 27, the .import command of the sqlite3 in /usr/bin named the first column id from the marked file. Some programs want the mark. Microsoft's support page for Excel states: "You can open a CSV file encoded with UTF-8 normally if it was saved with BOM (Byte Order Mark)." So find out what the receiving program expects before you remove anything. If it turns out to want the mark, the guide on converting to UTF-8 linked above shows how to add one.
How do I check whether a file has a BOM on a Mac?
Print the first three bytes of the file. head -c 3 takes them, and xxd, which is included with macOS, shows them in hex.
head -c 3 customers.csv | xxd
00000000: efbb bf ...
efbb bf is the mark. A file without one shows the first three characters of its text. For orders.csv, a CSV with no mark, head -c 3 orders.csv | xxd prints 6964 2c, which is the letters i and d and a comma.
Why the file command does not always report a BOM
file is the usual first look at an unknown file, and it names the mark for some files and not for others.
file customers.csv data.json
customers.csv: CSV text
data.json: Unicode text, UTF-8 (with BOM) text
Both files start with the same three bytes, and only one answer mentions them. The manual page describes the -k option as "Don't stop at the first match, keep going," and with it the rest of the answer for the CSV appears.
file -k customers.csv
customers.csv: CSV text
- , Unicode text, UTF-8 (with BOM) text, with CRLF line terminators
So an answer of CSV text from plain file is not proof that a CSV has no mark. The three bytes are the proof.
Why does  appear at the start of the file?
Some programs show the mark as three odd characters in front of the first word. That happens when the file is read as Windows-1252, in which the bytes EF, BB and BF are the characters ï, » and ¿. iconv reproduces it by reading the first five bytes of customers.csv that way.
head -c 5 customers.csv | iconv -f WINDOWS-1252 -t UTF-8
id
If you see  at the top of a file or in the first cell of a spreadsheet, the file is UTF-8 with a mark and the program read it in another encoding. If accented letters further down are wrong as well, fix garbled text and the wrong encoding covers the rest.
Check every file in a folder
The folder named exports holds three CSV files. This loop prints the first three bytes of each one beside its name.
for f in exports/*.csv
do
echo "$(head -c 3 "$f" | xxd -p) $f"
done
efbbbf exports/february.csv
efbbbf exports/january.csv
69642c exports/march.csv
Two of the three have the mark. To look through a folder and every folder inside it, grep lists each file that has the three bytes at the start of a line.
LC_ALL=C grep -rl $'^\xEF\xBB\xBF' exports
exports/february.csv
exports/january.csv
exports/archive/december.csv
Two details of that command come back in every command below. The $'...' quoting belongs to the shell, which turns \xEF\xBB\xBF into the three bytes before the tool sees them. LC_ALL=C in front makes the tool work on plain bytes and not on UTF-8 characters, so a file in any encoding can pass through it.
How do I remove a BOM from a UTF-8 file in Terminal?
Use sed. It is included with macOS, it can be told to look at the first line only, and it leaves a file that has no mark exactly as it was. Every command in this section is for a UTF-8 file. A UTF-16 file needs its mark, and a later section explains why.
Write a new file without the BOM
LC_ALL=C sed $'1s/^\xEF\xBB\xBF//' customers.csv > customers-clean.csv
Read the script from the left. 1s is a substitution on line 1 only, ^ ties it to the start of that line, and the three bytes are replaced with nothing. The original file is not touched, and nothing else changes on the way. The line endings and the accented letters of customers.csv came through byte for byte, and the new file is 82 bytes where the original is 85.
The same command was run on four files that have no UTF-8 mark: a plain CSV, a text file that ends without a line break, a Windows-1252 file and a UTF-16 file. Each came out with exactly the bytes it went in with. LC_ALL=C is what makes that true for the last two. Without it, sed read the Windows-1252 file as UTF-8 text, stopped with this message and wrote nothing.
sed: RE error: illegal byte sequence
Remove the BOM in place
LC_ALL=C sed -i .bak $'1s/^\xEF\xBB\xBF//' customers.csv
-i edits the file itself, and the extension after it names a backup. customers.csv loses its mark, and customers.csv.bak keeps the original. The manual page allows an empty extension, which keeps no backup, and advises against it because "you risk corruption or partial content in situations where disk space is exhausted." Delete the backup once you have checked the result. On a file that has no mark, the in-place form still writes the file again with the same bytes and still leaves a backup beside it.
Use tail only after a check
tail -c +4 copies a file from its fourth byte on. That removes a mark, and it equally removes the first three characters of a file that has none: on orders.csv it turned the header id,name,city into name,city. So test for the mark first.
if [ "$(head -c 3 customers.csv | xxd -p)" = "efbbbf" ]
then
tail -c +4 customers.csv > customers-clean.csv
fi
xxd -p prints the three bytes as plain hex, and tail runs only when they are the mark. The result is the same file that sed writes. For a file with no mark the test fails, tail does not run and no new file is written. tail copies bytes and never looks for lines, so the length of the first line does not matter to it.
Remove the BOM with awk
LC_ALL=C awk $'NR == 1 { sub(/^\xEF\xBB\xBF/, "") } { print }' customers.csv > customers-awk.csv
awk wrote the same file as sed in the tests, and three things make it the second choice. The bytes have to come from the shell's $'...' quoting: with \xEF\xBB\xBF inside ordinary quotes, the awk on macOS 27 left the mark where it was. awk ends every line it prints with a line break, so a file whose last line had none came out one byte longer. And it must not be pointed at a UTF-16 file: a 36-byte UTF-16 test file came out as 6 bytes. A UTF-8 file that has no mark and ends with a line break came out unchanged.
How do I remove the BOM from many files at once?
Both ways of removing the mark extend to a whole folder, and a job like this is where Terminal is the better tool. The first is one line, because sed -i treats each file as its own. In the words of the manual page, "line numbers in each file start at 1."
LC_ALL=C sed -i .bak $'1s/^\xEF\xBB\xBF//' exports/*.csv
Every .csv in the folder is written again, with or without a mark, and each one gets a .bak beside it. In the test folder the two marked files lost their marks, and march.csv, which had none, kept the same bytes.
The second is a loop that touches only the files that have a mark, and reports each one.
for f in exports/*.csv
do
if [ "$(head -c 3 "$f" | xxd -p)" = "efbbbf" ]
then
tail -c +4 "$f" > "$f.tmp" && mv "$f.tmp" "$f"
echo "removed the mark from $f"
fi
done
removed the mark from exports/february.csv
removed the mark from exports/january.csv
This loop keeps no backups, so run it on a copy of the folder the first time.
A BOM in the middle of a file
Files that are joined together bring their marks with them. Here are two of the exports joined with cat.
cat exports/january.csv exports/february.csv | xxd
00000000: efbb bf69 642c 746f 7461 6c0d 0a31 2c31 ...id,total..1,1
00000010: 300d 0aef bbbf 6964 2c74 6f74 616c 0d0a 0.....id,total..
00000020: 322c 3230 0d0a 2,20..
The second mark is in the second row of the dump, in front of the second header. The commands above take a mark from the start of a file only, so this one stays: piped through the same sed command, the joined text lost its first mark and kept its second. In that position the mark is no longer a signature. The Unicode FAQ's rule is that "when not at the beginning of a text stream, U+FEFF should normally not occur." Remove the marks before you join the files. For a file that already has one inside it, remove invisible characters from text has a command that deletes U+FEFF wherever it appears.
How do I make sure the BOM is gone?
Check three things: the mark is gone, nothing else changed, and the step that failed now works. The first is the same look at the first three bytes.
head -c 3 customers-clean.csv | xxd
00000000: 6964 2c id,
The file now starts with its text. For the second, compare the new file with the original, and skip the three bytes of the mark in the original.
cmp -i 3:0 customers.csv customers-clean.csv
The manual page for cmp describes -i as "Skip num1 bytes from file1, and optionally skip num2 bytes from file2," and notes that cmp "is silent if the files are the same." It printed nothing here, so everything after the mark is identical. For the third, run whatever failed. The lookup from the top of this guide now finds the first column.
awk -F, 'NR == 1 { for (i = 1; i <= NF; i++) col[$i] = i; print "id is column " col["id"] ", name is column " col["name"] ", city is column " col["city"] }' customers-clean.csv
id is column 1, name is column 2, city is column
The city column is still missing, because the line endings are a separate matter and removing the mark left them alone.
Should I remove the BOM from a UTF-16 file?
No. In UTF-16 the mark does a job that nothing else in the file does. Every character is stored in units of two bytes, the two bytes can be written in either order, and the mark at the start is how a reader learns which order the file uses. The Unicode FAQ's guideline for a file without one is: "If there is no BOM, the text should be interpreted as big-endian." A little-endian file that loses its mark is therefore read in the wrong byte order. export-utf16.txt is a small little-endian file, and its first two bytes are the mark FF FE.
xxd -l 8 export-utf16.txt
00000000: fffe 6900 6400 2c00 ..i.d.,.
file export-utf16.txt reads the mark and answers Unicode text, UTF-16, little-endian text. Now cut the two bytes off and ask again.
tail -c +3 export-utf16.txt > export-nomark.txt
file export-nomark.txt
export-nomark.txt: data
file no longer recognizes the file as text. iconv follows the guideline and reads it as big-endian.
iconv -f UTF-16 -t UTF-8 export-nomark.txt
椀搀Ⰰ渀愀洀攀 Ⰰ娀漀
Those characters are the same bytes paired in the wrong order, and iconv reported no error. When the next program cannot accept a mark and the file is UTF-16, the fix is to convert the file to UTF-8 and leave the UTF-16 mark alone. iconv reads the mark, uses it, and leaves it out of the result.
iconv -f UTF-16 -t UTF-8 export-utf16.txt > export-utf8.txt
The new file starts with id, and has no mark. The guide on converting a file to UTF-8 walks through that conversion and the encoding names that go with it.
One trap runs the other way. A UTF-8 file that still has its mark, converted to UTF-16, ends up with two.
iconv -f UTF-8 -t UTF-16 customers.csv | xxd -l 8
00000000: feff feff 0069 0064 .....i.d
The first FE FF is the mark iconv writes for UTF-16. The second is the UTF-8 file's own mark, converted as if it were a character of the text. With the UTF-8 mark removed first, the same conversion started with a single FE FF.
How do I remove a BOM in Caxton?
In Caxton the mark belongs to the file and is not a character in the text. Caxton sets it aside when it opens the file, shows whether the file has one, and writes it or leaves it out when it saves. Here is the walk with the same customers.csv, with the tier each step needs.
1. Open the file and see whether it has a mark
Drag the file onto Caxton, or choose File ▸ Open… and pick it. Three places show the mark.
- The right end of the status bar gives the encoding beside the line ending. A UTF-8 file with a mark reads UTF-8 (BOM), and one without reads UTF-8.
- View ▸ Show Inspector, or ⌥⌘I, opens the Inspector. In its Encoding section the BOM row reads Present or None, and the Why this encoding row gives the reason in a sentence: Caxton read this file as UTF-8 because it starts with the UTF-8 byte-order mark.
- The Text menu has a Byte Order Mark submenu with two items, Add and Remove. Only the one that applies is available: Remove for a file that has a mark, and Add for one that does not.
customers.csv opens as a table, and its first column is headed id with no mark attached, because the three bytes are not part of the text. A UTF-16 file reads UTF-16 LE or UTF-16 BE in the status bar with or without its mark, so for those files the BOM row is the place to look. Opening a file, the status bar and the Inspector are Caxton Free at any file size.
2. Choose Text ▸ Byte Order Mark ▸ Remove
The command changes no byte of the text. It changes what the next save writes. A banner confirms that the next save leaves the byte order mark out, the status bar now reads UTF-8, the BOM row reads None, and the document counts as edited. The change is one undo step, so ⌘Z puts the mark back. The command works the same way in the table and in the text view.
Removing the mark is an edit, so it follows the rule for editing. It is Caxton Free in a file that was at most 10 MB when it was opened, and Caxton Pro in a larger one. Every download includes a 7-day free trial of Caxton Pro with no credit card required. On Caxton Free a larger file still opens and the Inspector still shows its mark, and the command is refused with a banner that gives the reason and a License button.
3. Save with ⌘S
The mark sits in front of the text, so taking it away moves every byte after it. Caxton writes the whole file again without the three bytes and replaces the old file in one step. The banner that confirms the save gives that reason: the byte order mark changed. Edits you made to the text go into the same save. After the save, ⌘Z still works. It sets the mark to be written again, and the next save would put it back. Saving is not a separate Caxton Pro step. The rule applies to the edit, and a file that could be edited can be saved.
4. Check that it is gone
With the Inspector open, the BOM row reads None, and the Why this encoding row records what happened: Caxton read this file as UTF-8 because it started with the UTF-8 byte-order mark, which has since been removed. For a second opinion, the Terminal check from earlier in this guide looks at the saved file itself. Reading the Inspector is Caxton Free at any file size.
5. Or remove and save in one step with Save with Encoding
File ▸ Save with Encoding… does the removal and the save together. Its Encoding menu has separate entries for UTF-8 and UTF-8 (BOM). Choose UTF-8 for a file that has the mark, click Save, and the file is replaced at once without it. The save includes any unsaved edits, and nothing is left pending. To put the mark back, run the command again and choose UTF-8 (BOM). For UTF-16 the form has a checkbox named Start the file with a byte order mark. Save with Encoding follows the same rule as the other edits: Caxton Free up to 10 MB, and Caxton Pro above.
6. What happens with a UTF-16 or UTF-32 file
For a UTF-16 file, Remove asks before anything changes: Remove the byte order mark from this UTF-16 LE file? The sheet gives the reason, which is that the mark is how other programs know that the file is UTF-16 LE, and that without it many of them read the file as another encoding. Remove goes ahead, and Cancel leaves the document as it was. Caxton keeps reading the open window as UTF-16 either way. The tier is the one in step 2, since it is the same command.
A UTF-32 file gets the same question, and there the mark matters to Caxton as well. Caxton reads a file as UTF-32 only when it starts with the UTF-32 mark, and File ▸ Reopen with Encoding has no UTF-32 entry. A UTF-32 file saved without its mark cannot be read as UTF-32 in Caxton again, so leave that mark where it is.
Add and Remove are both unavailable for a file in an encoding that has no mark, such as Windows-1252, and for a new document that has no file yet, which gets its mark from Save with Encoding.
7. A mark that is not at the start of the file
A U+FEFF anywhere after the first position is part of the text, and Caxton treats it as an invisible character. The Invisibles row of the Inspector counts it as a stray byte-order mark, and Text ▸ Clean Up ▸ Remove Invisible Characters removes it after a preview. That command is also an edit under the same rule, and the invisible characters guide walks through it.
Which way of removing a BOM fits which job?
| The job | Tools included with macOS | In Caxton |
|---|---|---|
| You want to know whether a file has a mark. | head -c 3 piped to xxd prints the first three bytes. | The status bar and the Inspector's BOM row show it, on Caxton Free at any file size. |
| One UTF-8 file has to lose its mark. | sed removes it, and leaves a file that has no mark as it was. | Text ▸ Byte Order Mark ▸ Remove, then ⌘S. |
| A folder of files has to lose it, or the job repeats on a schedule. | sed -i or a short loop does every file in one command, and it is the better tool here. | Caxton changes the one file that is open in front of you. |
| The file is UTF-16 and the next program cannot take a mark. | iconv -f UTF-16 -t UTF-8 reads the mark and leaves it out of the UTF-8 result. | File ▸ Save with Encoding… converts the file to UTF-8. |
| A mark sits in the middle of a file. | The perl command in the invisible characters guide deletes it. | Text ▸ Clean Up ▸ Remove Invisible Characters removes it after a preview. |
| The receiving program wants the mark back. | printf and cat put the three bytes in front, as the UTF-8 guide shows. | Text ▸ Byte Order Mark ▸ Add sets it for the next save. |
Open the file, read the BOM row of the Inspector, and choose Text ▸ Byte Order Mark ▸ Remove to leave the mark out of the next save.
Download Caxton for Free7-day free trial of Caxton Pro, every feature, no credit card required. After it, Caxton Free keeps opening, searching and filtering at any size, and you can edit and save files up to 10 MB as text.
Frequently asked questions
Why do I see  at the start of my file?
Those three characters are a UTF-8 byte order mark read in the wrong encoding. The mark is the bytes EF BB BF, and in Windows-1252 those bytes are the characters ï, » and ¿. The file is UTF-8 with a mark, and the program that showed it read the file as Windows-1252. Remove the mark and the three characters go away. If accented letters further down are wrong as well, the program is still reading the file in the wrong encoding, and removing the mark does not change that.
Is it safe to remove the BOM from a UTF-8 file?
Yes for the text, which does not change. The Unicode Consortium's FAQ describes the mark in UTF-8 as a signature only, so removing it takes three bytes off the front of the file and leaves every character and every line ending as it was. In the tests for this guide, the file without its mark matched the original byte for byte from the fourth byte on. What can change is how a program guesses the encoding, because the mark was its hint that the file is UTF-8. Check what the receiving program expects, and keep a copy of the original until the import has worked.
Will Excel still open my CSV if I remove the BOM?
Microsoft's support page on opening UTF-8 CSV files in Excel states that a CSV file encoded with UTF-8 opens normally if it was saved with the byte order mark. For a file without the mark, the same page gives two other routes: Get Data from Text/CSV on the Data tab, or the Get Data From Text option, which opens the Text Import Wizard. So keep the mark on a CSV that people will open in Excel the ordinary way, and remove it from the copy that goes to an importer or a script that rejects it.
What should I do about a BOM in the middle of a file?
Remove it. The Unicode Consortium's FAQ states that U+FEFF should normally not occur when it is not at the beginning of a text stream. One way it gets there is joining two files that each start with a mark, which the cat example in this guide shows. The sed and tail commands in this guide take a mark from the start of a file only. To delete one further in, use a command that removes the character U+FEFF wherever it appears, or in Caxton choose Text ▸ Clean Up ▸ Remove Invisible Characters, which removes it after a preview that counts it as a stray byte-order mark.
Sources
- Caxton manual: Encodings, with Byte Order Mark, Save with Encoding and Reopen with Encoding
- Caxton manual: the Inspector
- Unicode Consortium: UTF-8, UTF-16, UTF-32 and BOM FAQ
- Python documentation: codecs, with the utf-8 and utf-8-sig codecs
- Python documentation: json, JSON encoder and decoder
- RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format
- Snowflake documentation: CREATE FILE FORMAT and its SKIP_BYTE_ORDER_MARK option
- Google Cloud documentation: Load CSV data from Cloud Storage into BigQuery
- Microsoft Support: Opening CSV UTF-8 files correctly in Excel
- Apple's manual pages on macOS:
man sed,man tail,man file,man cmpandman xxd, read with the shipped tools in Terminal