手册页标题中的数字,例如 sleep(3)
Number in man page titles e.g. sleep(3)

原始链接: https://lalitm.com/til-number-in-man-page-titles-e-g-sleep-3/

许多Linux系统程序员经常查阅man页来获取文档。作者最近发现与函数名一起出现的数字(例如`sleep(3)`或`read(2)`)的意义。这些数字并非随机——它们指示了文档的*位置*。 根据`man man`,第2节记录系统调用(内核函数),第3节涵盖库调用(程序库内的函数),第1节详细介绍可执行程序/shell命令。最近的代码审查指出了作者的误解,将对库函数的引用从`man 2`更正为`man 3`。 此外,附加在数字后面的字母表示特定的标准——“p”代表POSIX,“x”代表X文档,为man页系统增加了另一层细节。作者承认自己使用了多年却不了解文档组织的关键方面。

## 黑客新闻讨论:Man页面冷知识与彩蛋 一篇关于`man`页面(Unix手册页)冷知识的文章引发了黑客新闻的讨论,揭示了许多有趣细节和怀旧情怀。用户分享了关于隐藏功能的故事,例如为名为Tyler的用户定制的关机消息,以及这些彩蛋背后的故事——一位开发者希望为系统增加一些“乐趣”。 讨论探讨了彩蛋是否适合像`man`这样普及的软件,观点从赞扬它们是快乐的来源到质疑其安全隐患和测试需求不等。许多人指出,旧系统充满了这样的隐藏惊喜,但由于对安全性和全面测试的日益重视,它们已经变得不那么常见。 讨论还涉及了`man`页面中常常令人困惑的编号系统(例如`man 3`),追溯其起源到AT&T System V文档的物理文件夹组织方式。最后,用户们争论了在Stack Overflow和LLM时代`man`页面的相关性,一些人建议使用对话界面来访问文档。
相关文章

原文

If you do Linux systems programming, you will have likely pored over man pages, either on the command line or, my personal preference, using the excellent man7.org or linux.die.net. I’ve always seen the numbers in sleep(3) and read(2) and idly wondered what they meant, but never actually bothered to look them up.

That is, until a review comment on a pull request:

// Behaves like man 2 basename

reviewer: nit: it’s not a syscall, so “man 2” is incorrect

So I looked it up. The answer was in the man(1) page (also accessible via the delightful man man command):

The table below shows the section numbers of the manual followed by the types of pages they contain.

  1   Executable programs or shell commands
  2   System calls (functions provided by the kernel)
  3   Library calls (functions within program libraries)
  (... less common section numbers)

So my colleague was right and the code should have read // behaves like man 3 basename as basename(3) is a libc library call.

And there’s more to these numbers than what the man man page describes: various letters can also be attached as suffixes to the numbers. For example, p indicates a POSIX man page like with free(3p) and x indicates X documentation like with ncurses(3x).

It’s embarrassing to admit I’ve been using man pages for years but only learned this today.

联系我们 contact @ memedata.com