$_ CLI In Depth
Learning progress 0%
lesson text viewing editors

Text Encodings, UTF-8 & Line Endings

5 min read Module 3: Viewing Files & Text Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:20

Bytes first, characters second

A disk stores bytes. A text editor or a program decides how to interpret them. In ASCII, each byte from 0 to 127 is one character, and the first 128 code points are the same in almost every encoding in common use. UTF-8 keeps those 128 values as single bytes, so an ASCII file is also a valid UTF-8 file. For any other character, UTF-8 uses a sequence of two to four bytes, with a pattern in the high bits that tells a decoder where each character starts.

This design has two practical consequences. First, a tool that counts bytes and a tool that counts characters will disagree on any file with non-ASCII text. Second, a tool that splits a file at a fixed byte offset can break a character in half, leaving bytes that are not valid text. The fix is to work with characters when the meaning matters and with bytes only when the format is binary or the count is about storage.

Seeing the bytes of a UTF-8 file

The lab can show you the bytes directly. The letter e with an acute accent is written in UTF-8 as two bytes, 0xC3 followed by 0xA9. The command printf can write those bytes with octal escapes, and wc -c then counts them.

BASH4 lines
cd /workspace
printf 'caf\303\251\n' > cafe.txt
wc -c cafe.txt
od -c cafe.txt

The word has four letters and a newline, so it is five characters, but the byte count is six, because the accented letter takes two bytes. The od -c output shows the bytes as escape sequences. Octal values, which the next unit covers, make such bytes easy to read.

Line endings: LF, CRLF and the rest

Unix tools end a line with a single line feed, which is byte 0x0A. Windows text traditionally ends a line with a carriage return and a line feed, bytes 0x0D and 0x0A. The carriage return is invisible in most terminals, so a file with CRLF endings looks normal until you ask a tool that counts or compares bytes. A shell script with CRLF endings often fails with an error that mentions a command not found, because the interpreter name ends in an invisible carriage return.

Old Mac files used only a carriage return, which is rare today. Most current editors can read all three forms, but programs that do not expect them may not. The file utility reports which style a file uses, and the od command shows the raw bytes if you need to be certain.

BASH4 lines
cd /workspace
printf 'dos line\r\n' > dos.txt
file dos.txt
tr -cd '\r' < dos.txt | wc -c

The first command reports the line terminators. The second deletes everything except carriage returns and counts what is left, which is the number of carriage returns in the file.

Byte order marks

Some editors write a byte order mark at the start of a UTF-8 file. It is the three bytes 0xEF 0xBB 0xBF, which decode as the zero-width character U+FEFF. Many Unix tools do not expect it. A script whose first line begins with a byte order mark fails at the interpreter line, because the kernel sees the mark before the hash-bang. Remove the mark when you see an odd first-line error, using an editor that saves without it.

Warning

Do not convert line endings with a blunt search and replace that changes every carriage return in a binary file. Check first with file that the file is text, and keep a backup before any conversion.

Choosing a practical rule

Keep your own files in UTF-8 with LF line endings, which is the default on Linux. When you receive a file from another system, check it with file before you process it. When a tool misbehaves on a file that looks fine, look for CRLF, a byte order mark, or a stray non-ASCII character, in that order. Most problems are one of these three.

Locale settings determine how tools interpret characters. A tool running under the C locale treats each byte as a character, which is fast and predictable for ASCII data. Under a UTF-8 locale, it counts multibyte characters. The lab environment is set up in a simple way, so the byte counts in the examples are the same as the character counts for ASCII data.

Invalid bytes and what tools do with them

A file can claim to be UTF-8 and still contain bytes that are not valid UTF-8, for example a Latin-1 accented letter saved by an old program. Tools handle such bytes differently. grep may report that a file is binary and print only a summary, while cat copies the bytes unchanged. The option -a of grep forces it to treat the file as text, which is useful when you need to see the lines around the invalid bytes.

When you need to convert between encodings, the iconv program does the work if it is installed, and it takes the source and target encodings as options. Convert a copy, never the original, and check the result with file afterward. A conversion that silently drops characters is worse than no conversion, so read the warnings it prints.

The safest approach is to fix the source of the problem. Ask the program that produces the file to write UTF-8, and keep the conversion for files that you cannot regenerate. A pipeline that handles encodings well is short, and each stage has a clear encoding on its input and output.

Practice in the lab

The lab creates a UTF-8 file and a file with CRLF endings, then asks you to count the bytes, count the carriage returns, and identify the file with the file utility. Each result goes into a text file for the checker to compare.

Characters to bytes: UTF-8 and line endings

Visualizer

Files hold bytes. See how a code point becomes UTF-8 bytes, and why a Windows line ending breaks shell scripts.

1 / 4
character
é
code point
U+00E9
decimal
233

Unicode gives é the number 0xE9. It is above 0x7F, so it cannot be a single ASCII byte.

printf "\xc3\xa9\n" | od -An -tx1 -c
Terminal lab · 3 steps · real Linux in your browser

Measure bytes and line endings

Count the bytes of a UTF-8 word, count carriage returns, and identify a CRLF file.

01
Count bytes of a UTF-8 word
The file utf8.txt holds the word café and a newline. Save its size in bytes: ``bash wc -c < utf8.txt > bytes.txt ``
Show hint for step 1
The accented letter takes two bytes in UTF-8.
02
Count carriage returns
The file dos.txt uses CRLF endings. Count its carriage returns by deleting every other byte and counting what is left: ``bash tr -cd '\r' < dos.txt | wc -c > cr.txt ``
Show hint for step 2
tr -cd keeps only the characters you name, and the complement option makes that set the ones to delete.
03
Identify the line endings
Save the report that file gives for dos.txt: ``bash file dos.txt > kind.txt ``
Show hint for step 3
file names the line terminators it finds.
Check yourself Question 1 of 4
Concept Check Unix line ending recall

Which byte sequence ends a line in ordinary Unix text?

Recap & Key Takeaways

UTF-8 keeps ASCII as single bytes, and other characters take two to four bytes, so byte and character counts can differ.
Unix lines end with LF, while Windows text ends with CRLF, and a stray carriage return can break scripts.
A byte order mark at the start of a file can break a script whose first line must be a hash-bang.
The file utility identifies the encoding and line style, and od shows raw bytes when you need certainty.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue