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

user@hostname ~ $ man ls | grep "BU"
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?

  • This is part of the reason I hate BSD man. It formats the text and runs the pager even when its output is a pipe. And my pager is vim, so Linus forbid I ever did man foo | grep bar, I get a nonresponsive pipeline (and maybe a messed up terminal to boot). :/ mandb man, which is what you usually see on Linux, is more sensible. – muru Jun 26 '18 at 2:17
  • unix.stackexchange.com/questions/371062 is also a MacOS question. – JdeBP Jun 26 '18 at 12:13

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:


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
  • 2
    Thanks Scot! Redirecting to a file and opening in a text editor should have been the first thing I tried. Using that information, and the information from unix.stackexchange.com/a/15866 (i.e. man ls | col -b | grep "BUGS") I was able to get what I wanted. – Jonathan Merklin Jun 25 '18 at 22:28
  • 7
    Holy moly, the bold is the old TTY and typewriter era, type a letter and backspace and type the letter again, knowing they won’t line up perfectly and will deposit more ink. There’s got to be a nroff command to translate that if you need to grep - would you mind if I expanded this with how to pass the correct command to groff via man ? – bmike Jun 25 '18 at 22:59
  • @Kroltan +10 and +10 to scot as well. That's way more elegant than how I wasn't thinking of skinning this particular cat. – bmike Jun 26 '18 at 3:09
  • @Kroltan Hmmm - for me, man -P cat ls | grep BUGS works identically to man ls | grep BUGS, both return nothing. – Scot Jun 26 '18 at 5:31
  • 1
    For manual systems that employ GNU roff, there are indeed options to grotty that will stop it from emitting either TTY-37 or ECMA48 control sequenes. – JdeBP Jun 26 '18 at 13:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .