Why does cat not use options the way I expect UNIX programs to use switches

catcommand lineunix

I have been a UNIX user for more years than I care to think about, and in that time I have been trained to expect that when contradictory switches are given to a program the last one wins. Recently I have noticed that

cat -bn file

and

cat -nb file

both use the -b option (number non-blank lines) over the -n option (number all lines). I get this behavior on both BSD and Linux, so I don't think it is an implementation quirk. Is this something that is specified somewhere and am I just crazy for expecting the first example to number all lines?

Best Answer

I took a look at the FreeBSD source code for cat(1), and the relevant source lines are:

case 'b':
    bflag = nflag = 1;  /* -b implies -n */

So this looks like a deliberate design decision; the interpretation of -b is that it modifies the behavior of -n, rather than -b and -n being two mutually exclusive alternatives.

Related Question