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.
cd /workspace
printf 'caf\303\251\n' > cafe.txt
wc -c cafe.txt
od -c cafe.txtThe 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.
cd /workspace
printf 'dos line\r\n' > dos.txt
file dos.txt
tr -cd '\r' < dos.txt | wc -cThe 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.
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.