UTF-8(7)
๐ UTF-8(7) ๐
okay so bestie, welcome to the UTF-8(7) page from the Miscellaneous Information Manual, and honestly? this one is a banger. it's about an ASCII compatible multibyte Unicode encoding, and if you've ever wondered why emoji work on your terminal but '\0' doesn't break your filenames, this is the lore. ๐ฆ
โจ NAME โจ
UTF-8 โ an ASCII compatible multibyte Unicode encoding. that's it, that's the whole vibe. ๐
๐ธ DESCRIPTION ๐ธ
so here's the tea. the Unicode 3.0 character set chills in a 16-bit code space. the most obvious way to encode Unicode (aka UCS-2) is just... a sequence of 16-bit words. sounds fine, right? it's not. ๐ญ
because those strings can contain โ as part of many 16-bit characters โ bytes like '\0' or '/', and those two are literally the main characters in filenames and in arguments to other C library functions. they mean something. you can't just let them show up randomly.
and on top of that, the majority of UNIX tools expect ASCII files and can't read 16-bit words as characters without major modifications. it's giving "not compatible, not invited." ๐
for these reasons, UCS-2 is not a suitable external encoding of Unicode in filenames, text files, environment variables, and so on. the ISO/IEC 10646 Universal Character Set (UCS), a superset of Unicode, occupies an even larger code space โ 31 bits โ and the obvious UCS-4 encoding for it (a sequence of 32-bit words) has the same problems. no growth, no glow-up. ๐
the UTF-8 encoding of Unicode and UCS does not have these problems and is the common way in which Unicode is used on UNIX-style operating systems. periodt. ๐
๐ Properties
the UTF-8 encoding has the following nice properties, and bestie they are so nice:
- UCS characters
0x00000000to0x0000007f(the classic US-ASCII characters) are encoded simply as bytes0x00to0x7f(ASCII compatibility). this means that files and strings which contain only 7-bit ASCII characters have the same encoding under both ASCII and UTF-8. slay, backwards compatible. - All UCS characters greater than
0x7fare encoded as a multibyte sequence consisting only of bytes in the range0x80to0xfd, so no ASCII byte can appear as part of another character and there are no problems with, for example,'\0'or'/'. iconic behaviour honestly. - The lexicographic sorting order of UCS-4 strings is preserved. ๐ซถ
- All possible 2^31 UCS codes can be encoded using UTF-8.
- The bytes
0xc0,0xc1,0xfe, and0xffare never used in the UTF-8 encoding. they're simply not invited to the function. ๐ - The first byte of a multibyte sequence which represents a single non-ASCII UCS character is always in the range
0xc2to0xfdand indicates how long this multibyte sequence is. All further bytes in a multibyte sequence are in the range0x80to0xbf. This allows easy resynchronization and makes the encoding stateless and robust against missing bytes. robust queen. ๐ง - UTF-8 encoded UCS characters may be up to six bytes long, however the Unicode standard specifies no characters above
0x10ffff, so Unicode characters can be only up to four bytes long in UTF-8. lowkey an important distinction, highkey the whole point.
๐ฆ Encoding
These are the byte sequences used to represent a character. which sequence you use depends on the UCS code number of the character, bestie:
0x00000000 - 0x0000007F:
0xxxxxxx
0x00000080 - 0x000007FF:
110xxxxx 10xxxxxx
0x00000800 - 0x0000FFFF:
1110xxxx 10xxxxxx 10xxxxxx
0x00010000 - 0x001FFFFF:
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
0x00200000 - 0x03FFFFFF:
111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
0x04000000 - 0x7FFFFFFF:
1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
The xxx bit positions are filled with the bits of the character code number in binary representation, most significant bit first (big-endian). Only the shortest possible multibyte sequence which can represent the code number of the character can be used. shortest form only, no cheating. ๐ซ
The UCS code values 0xd800โ0xdfff (UTF-16 surrogates) as well as 0xfffe and 0xffff (UCS noncharacters) should not appear in conforming UTF-8 streams. According to RFC 3629 no point above U+10FFFF should be used, which limits characters to four bytes.
no cap, if you're emitting surrogate ranges or noncharacters into your UTF-8 stream, you're not doing conforming UTF-8 and the standards committee is looking at you with concern. ๐ญ
๐ Example
The Unicode character 0xa9 = 1010 1001 (the copyright sign) is encoded in UTF-8 as
11000010 10101001 = 0xc2 0xa9
and character 0x2260 = 0010 0010 0110 0000 (the "not equal" symbol) is encoded as:
11100010 10001001 10100000 = 0xe2 0x89 0xa0
see? cute little byte stacks. main character energy. ๐
๐ Application notes
Users have to select a UTF-8 locale, for example with
export LANG=en_GB.UTF-8
in order to activate the UTF-8 support in applications. no locale, no UTF-8, it's that simple babe.
Application software that has to be aware of the used character encoding should always set the locale with for example
setlocale(LC_CTYPE, "")
and programmers can then test the expression
streq(nl_langinfo(CODESET), "UTF-8")
to determine whether a UTF-8 locale has been selected and whether therefore all plaintext standard input and output, terminal communication, plaintext file content, filenames, and environment variables are encoded in UTF-8. that's your vibe check for the whole environment. โ
Programmers accustomed to single-byte encodings such as US-ASCII or ISO/IEC 8859 have to be aware that two assumptions made so far are no longer valid in UTF-8 locales:
- Firstly, a single byte does not necessarily correspond any more to a single character. ๐ณ
- Secondly, since modern terminal emulators in UTF-8 mode also support Chinese, Japanese, and Korean double-width characters as well as nonspacing combining characters, outputting a single character does not necessarily advance the cursor by one position as it did in ASCII.
Library functions such as mbsrtowcs(3) and wcswidth(3) should be used today to count characters and cursor positions. stop counting bytes with your bare hands bestie, use the actual functions. ๐ซถ
The official ESC sequence to switch from an ISO/IEC 2022 encoding scheme (as used for instance by VT100 terminals) to UTF-8 is ESC % G ("\x1b%G"). The corresponding return sequence from UTF-8 to ISO/IEC 2022 is ESC % @ ("\x1b%@"). Other ISO/IEC 2022 sequences (such as for switching the G0 and G1 sets) are not applicable in UTF-8 mode.
๐ฅ Security
The Unicode and UCS standards require that producers of UTF-8 shall use the shortest form possible, for example, producing a two-byte sequence with first byte 0xc0 is nonconforming. Unicode 3.1 has added the requirement that conforming programs must not accept non-shortest forms in their input.
This is for security reasons, and it's kind of a big deal: if user input is checked for possible security violations, a program might check only for the ASCII version of "/../" or ";" or NUL and overlook that there are many non-ASCII ways to represent these things in a non-shortest UTF-8 encoding.
๐จ bestie. this is the security chapter, so listen. if you only validate the ASCII spelling of a path traversal or a semicolon or a NUL, an attacker can hand you a non-shortest UTF-8 encoding of the same thing and walk right in. always check the shortest form rule. this is not a drill. ๐จ
โญ Standards
ISO/IEC 10646-1:2000, Unicode 3.1, RFC 3629, Plan 9.
๐ SEE ALSO
and that's the page!! you now know UTF-8 better than most people who use it every day, which is honestly iconic of you. ๐
this has been brought to you by Linux man-pages 6.20, dated 2026-02-22, and the UTF-8(7) entry in the Miscellaneous Information Manual. go forth and encode responsibly, bestie. ๐โจ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: GPL-2.0-or-later
- Copyright 1996-2001, Markus Kuhn <mkuhn@acm.org>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms