Skip to main content

nl (Number Lines)

Explanation

The nl command reads lines from a file and outputs them with line numbers added. While cat -n can also number lines, nl provides more advanced formatting options, such as numbering only non-empty lines (the default behavior) or using specific numbering styles.

Basic Syntax

nl [OPTION]... [FILE]...

Common Flags and Options

FlagDescription
-b, --body-numbering=STYLEUse STYLE for numbering body lines (e.g., a for all, t for non-empty (default)).
-n, --number-format=FORMATInsert line numbers according to FORMAT (ln, rn, rz).
-w, --number-width=NUMBERUse NUMBER columns for line numbers.
-s, --number-separator=STRINGAdd STRING after (possible) line number.

Real-world Examples

  1. Number non-empty lines in a file (default):

    nl script.sh
  2. Number all lines, including empty ones:

    nl -b a script.sh
  3. Format line numbers with leading zeros:

    nl -n rz script.sh