Start with the default rule
The lab is a six-line synthetic incident record with one deliberately empty line. Work in a directory where you may create nl-demo. mkdir creates it; && runs the next command only if the previous one succeeds; cd makes it the working directory. The printf format '%s\n' prints each following quoted argument followed by a newline. A backslash immediately before a displayed newline is Bash continuation syntax, so the next physical line remains part of the same command. The shell's > redirection creates incident.txt from printf's output. The printf article develops this safer separation of format from data.
With no numbering-style option, nl treats ordinary input as a logical-page body and numbers only nonempty body lines. -w 3 reserves at least three columns for a number. -s ': ' changes the default tab separator to a colon and a space; the single quotes pass those two characters as one argument. incident.txt is the input operand.
mkdir nl-demo &&
cd nl-demo &&
printf '%s\n' \
'INFO 07:40 sensor ready' \
'FAIL 07:41 auth alice' \
'' \
'WARN 07:42 clock skew' \
'FAIL 07:43 sudo bob' \
'INFO 07:44 capture closed' > incident.txt &&
nl -w 3 -s ': ' incident.txt 1: INFO 07:40 sensor ready
2: FAIL 07:41 auth alice
3: WARN 07:42 clock skew
4: FAIL 07:43 sudo bob
5: INFO 07:44 capture closedThe empty third input line is not reproduced byte for byte. GNU nl writes a blank three-column number field plus two placeholder spaces where the ': ' separator would appear, so that output row contains five spaces before its newline. It does not advance the counter. Treat nl output as a transformed view: these are ordinals of selected lines, not original input-line addresses.
Make the presentation contract explicit
The -b option selects the body numbering style. Its argument a means all lines, including empty ones; the default style t means only nonempty text lines, and n means none. In -n rz, -n chooses the number format and rz means right-justified with leading zeroes. The other portable formats are rn, right-justified without leading zeroes, and ln, left-justified without leading zeroes. -w 3 supplies the minimum width, and -s ' | ' supplies a visible separator.
Numbering transforms the stream. It adds prefixes, replaces an empty line with a prefix plus separator when -ba is active, and can make later text consumers see different fields. Save the unmodified acquisition separately; produce a numbered view for discussion. If bytes matter, the od article shows how to inspect them rather than trusting their screen rendering.
nl -ba -w 3 -n rz -s ' | ' incident.txt001 | INFO 07:40 sensor ready
002 | FAIL 07:41 auth alice
003 |
004 | WARN 07:42 clock skew
005 | FAIL 07:43 sudo bob
006 | INFO 07:44 capture closedWidth is a minimum, not a truncation rule: numbers wider than three digits expand the field. Do not parse a report by fixed byte offsets unless that expansion is part of the contract.
Selective numbering creates a new ordinal
A numbering style beginning with p numbers only lines that match the following basic regular expression, or BRE. The quoted argument -bp'^FAIL' is one shell word: -b selects the body style, p introduces the BRE, ^ anchors it at the beginning of a line, and FAIL is literal text. Single quotes prevent the shell from interpreting regex punctuation. See the grep article for BRE rules and the effect of anchors.
-v 100 chooses 100 as the first number for this logical page. -i 10 increments the counter by ten after each numbered line. Nonmatching lines are still reproduced, but do not advance it. The labels 100 and 110 are review labels for the two matching records; they do not mean input lines 100 and 110, and they do not reveal that the records originated on physical lines 2 and 5.
nl -bp'^FAIL' -v 100 -i 10 -w 3 -s ': ' incident.txt INFO 07:40 sensor ready
100: FAIL 07:41 auth alice
WARN 07:42 clock skew
110: FAIL 07:43 sudo bob
INFO 07:44 capture closedPattern interpretation is locale-sensitive and BRE is not an input validator. Set an appropriate locale and test the deployed implementation before making these labels part of an evidence or automation protocol.
Blank-line grouping is stateful
The next file models a report in which adjacent blank lines are meaningful separators. Inside the single-quoted printf format, the shell passes each \n escape unchanged and printf turns it into a newline. After INFO A, three newline escapes end that text line and create two empty lines; after WARN B, two newline escapes create one empty line. The final escape ends INFO C, producing exactly six input lines. With -ba active, -l 2 says to treat each group of two adjacent empty lines as one numbered line. It does not mean number two blank lines, and it does not collapse or remove either input line.
The first blank in each run is reproduced with an unnumbered prefix; the second blank in the first run receives number 2. The single blank after WARN B is not numbered. Any nonempty line ends the run, so the grouping counter restarts before the next blank line.
printf 'INFO A\n\n\nWARN B\n\nINFO C\n' > gaps.txt &&
nl -ba -l 2 -w 2 -s ': ' gaps.txt 1: INFO A
2:
3: WARN B
4: INFO CPOSIX specifies this behavior: -l 2 numbers only the second adjacent blank line when the section's style is a. A recent alternative implementation should be tested here rather than assumed equivalent because the rule depends on state across lines and section boundaries.
Logical-page sections reset the counter
nl has an older document model that remains surprisingly useful. A logical page has header, body, and footer sections. A line containing exactly \:\:\: starts a header, \:\: starts a body, and \: starts a footer. The backslashes are data here because they are inside single-quoted printf arguments. nl consumes each delimiter line and emits an empty output line in its place.
The fixture below wraps incident records in two synthetic briefings. -h a numbers all header lines, -b a all body lines, and -f a all footer lines. These options give the three sections independent styles, not independent counters. GNU nl uses one numbering sequence and resets that counter at every section-delimiter transition unless -p disables the resets. Without -p here, every header, body, and footer therefore begins at 1.
printf '%s\n' \
'\:\:\:' 'INCIDENT IR-482' \
'\:\:' \
'INFO 07:40 sensor ready' \
'FAIL 07:41 auth alice' \
'' \
'WARN 07:42 clock skew' \
'FAIL 07:43 sudo bob' \
'\:' 'Owner: blue' \
'\:\:\:' 'INCIDENT IR-483' \
'\:\:' 'INFO 09:00 no related events' \
'\:' 'Owner: blue' > briefing.txt &&
nl -ha -ba -fa -w 2 -s ': ' briefing.txt
1: INCIDENT IR-482
1: INFO 07:40 sensor ready
2: FAIL 07:41 auth alice
3:
4: WARN 07:42 clock skew
5: FAIL 07:43 sudo bob
1: Owner: blue
1: INCIDENT IR-483
1: INFO 09:00 no related events
1: Owner: blueThe delimiter syntax can collide with real data: a line exactly equal to a delimiter is control input and will not survive literally. -d CC can replace the two delimiter characters, but it cannot change the one/two/three repetition pattern. Inspect supplied data before applying this document model.
Keep one sequence across sections with -p
The -p option disables the counter resets at logical-page delimiters. The header, body, and footer style rules still decide which lines receive numbers, and delimiter lines are still replaced by empty lines, but the one numbering sequence continues at the next numbered line.
This is useful when a reviewer needs one stable discussion sequence across a formatted briefing. It is not a recovery of original file offsets: delimiter lines vanished from the view, and the numbered blank body line still participates.
nl -p -ha -ba -fa -w 2 -s ': ' briefing.txt
1: INCIDENT IR-482
2: INFO 07:40 sensor ready
3: FAIL 07:41 auth alice
4:
5: WARN 07:42 clock skew
6: FAIL 07:43 sudo bob
7: Owner: blue
8: INCIDENT IR-483
9: INFO 09:00 no related events
10: Owner: blueIf another tool must address the source by physical line, generate that mapping before nl consumes delimiters or filters numbering. A display label and a source coordinate answer different questions.
A no-number style still reads the file
A common diagnostic use also marks a delegation boundary. Create a harmless synthetic file. -b n selects no numbering for body lines, -w 1 makes the unused number field one column wide, and -s '' supplies an empty separator. nl still opens and reproduces the file, prefixing one space to each unnumbered line in this GNU execution.
That means a policy which permits nl to run with a more privileged identity may also permit reading files available to that identity. GTFOBins records this file-read capability for sudo and set-user-ID contexts. It is not, by itself, a vulnerability in nl and it is not byte-transparent: the added spaces are proof that this output is a transformed view. The unsafe condition is granting a general file-reading program access to data the caller should not read.
printf '%s\n' \
'token=DEMO-NOT-SECRET' \
'scope=training-only' > secret.fixture &&
nl -bn -w 1 -s '' secret.fixture token=DEMO-NOT-SECRET
scope=training-onlyAudit sudo rules and wrappers by capability, not by a command's familiar name. Restrict exact operands where possible, avoid elevated generic viewers, and never use real secrets in a demonstration.
Treat portability as an executable contract
The short options used above belong to the POSIX XSI nl interface: section styles, delimiters, increment, blank grouping, number format, no-renumber, separator, start value, and width. POSIX names at most one file operand. GNU Coreutils additionally accepts long option names and multiple files, treating those files as one document rather than resetting between them. Scripts intended for mixed Unix systems should stay with one file or standard input and the short forms.
These examples were executed with GNU Coreutils nl 9.7 under the C.UTF-8 locale. The current GNU manual documents the same logical-page model. Rust uutils Coreutils 0.12.0 made compatibility especially timely, and its nl exposes the same main option vocabulary; the project still warns that some behavior can differ. The uutils binary was not present in this test environment, so page resets, blank grouping, regex locale behavior, and exact unnumbered prefixes remain parity tests for any uutils deployment rather than claims made here.
Write the contract before the command: which section styles count, whether blank lines count, whether labels reset, what locale interprets the BRE, and whether the output is merely a view or must preserve source bytes. Then test that contract on the exact implementation in production. nl is dependable when its numbers are treated as chosen annotations—not as facts that were already in the input.