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
| Flag | Description |
|---|---|
-b, --body-numbering=STYLE | Use STYLE for numbering body lines (e.g., a for all, t for non-empty (default)). |
-n, --number-format=FORMAT | Insert line numbers according to FORMAT (ln, rn, rz). |
-w, --number-width=NUMBER | Use NUMBER columns for line numbers. |
-s, --number-separator=STRING | Add STRING after (possible) line number. |
Real-world Examples
-
Number non-empty lines in a file (default):
nl script.sh -
Number all lines, including empty ones:
nl -b a script.sh -
Format line numbers with leading zeros:
nl -n rz script.sh