Viewing and processing text

Command nl

Output file with line numbers

Linux · Bash examples · commands run on the selected server

When to use nl

Use nl to add line numbers to text output without editing the source file. Choose whether blank lines should be numbered and how the number column should be formatted.

Syntax

nl [OPTIONS] [FILE]

Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.

Common options and arguments

-b a
Number all lines, including empty ones.
-w N
Number field width.
-s TEXT
Separator between number and line.

Usage examples

Number non-blank lines

nl ./notes.txt

The default mode numbers nonempty body lines.

Number all lines

nl -ba ./notes.txt

Every body line, including blank lines, receives a number.

Set up number format

nl -ba -w3 -s': ' ./notes.txt

A narrow number field and explicit separator produce a more compact display.

Notes and limitations

  • By default, empty body lines are not numbered.
  • Special logical page separators can reset the numbering; Take this into account when processing free text.

Common questions and troubleshooting

Why are empty lines not numbered?

The default body-numbering style skips empty lines. Select the all-lines style when you need a number on each line.

Does nl save numbers into the file?

No. It writes a transformed stream. Redirect to a separate file if you need a numbered copy and avoid overwriting the input.

Further help

Options vary by version and distribution. Check the installed documentation with this command: man nl.