macOS – Why Section Titles of Man Pages Aren’t Greppable

command linemacosmanopen sourceterminal

This was tested in El Capitan and in a colleague's High Sierra, in the standard Terminal (bash).

user@hostname ~ $ man ls | grep "BU"
BUGS
user@hostname ~ $ man ls | grep "BUG"
user@hostname ~ $ 
user@hostname ~ $ man ls | grep "IEEE"
     files in order to be compatible with the IEEE Std 1003.2 (``POSIX.2'')
     The ls utility conforms to IEEE Std 1003.1-2001 (``POSIX.1'').

To clarify: "BUGS" is a section title in that (and various other) manpages. For section titles, grepping only seems to work for the first 2 characters; this is consistent across a few different section titles we tried. For the rest of the content, grep seems to work as expected.

I ssh'd into a non-BSD flavored Linux box (Amazon Linux) and it does not appear to exhibit the same behavior.

What's going on here?

Best Answer

You can see what is happening if you view the raw codes within a man page. One way to do this is to export the man page to a file and inspect its contents directly:

man ls > man.ls
nano man.ls

The word "BUGS" actually looks like this in the file:

B^HBU^HUG^HGS^HS

You'll see that the headers contain formatting characters, so the entire word "BUGS" is not present.


If you want to access the plaintext contents of the manual page, you can use the command

man -P cat <thepage>

The -P option sets the pager on other unix and cat will ignore the formatting information, giving a plaintext output. However, this doesn't appear to work on macOS, so the output needs a manual col -b step in the pipeline:

man ls | col -b | grep BUGS