How does `cat -b` differ from `cat -n`?

QuestionsQuestions4 SkillsProDec, 19 2025
0157

Great question! While both options add line numbers, the difference lies in how they treat blank (empty) lines.

Here is the breakdown:

1. cat -n (Number all)

  • Action: Numbers every single line, regardless of whether it has text or is completely empty.
  • Use case: Best when you need to know the exact total line count or reference a specific line regardless of content.

2. cat -b (Number non-blank)

  • Action: Numbers only the lines that contain text. It skips numbering for empty lines.
  • Use case: Best when you are reading code or text where blank lines are just for spacing and you only care about the lines with actual data.

Comparison Example

Imagine a file with this content (a word, a blank line, and another word):

Hello

World

Using cat -n:

     1  Hello
     2  
     3  World

(Notice line 2 is numbered even though it is empty.)

Using cat -b:

     1  Hello
        
     2  World

(Notice the blank line is skipped, and "World" becomes line 2.)

Summary Table

Option Numbers Blank Lines? Description
-n Yes Numbers all output lines.
-b No Numbers non-blank lines only.

Note: If you use both at the same time (cat -nb), -b actually takes priority, and blank lines will not be numbered.

0 Comments

no data
Be the first to share your comment!