Chapter 20
In This Chapter
print doesn't work, and what doeslp and lpr: sending a joblpq, lpstat, lprm, cancel, lpc: managing the queuefmt | pr | lpPrinting is the last thing most shell books cover and the first thing people actually need when they're automating an office. Invoices. Packing slips. Shift rosters. Nightly reports that somebody wants on a clipboard, not on a screen.
Windows makes this stranger than it needs to be. Try the obvious thing:
C:\> print report.txt
Unable to initialize device PRN
That's Windows' own print.exe, still talking to PRN — a DOS device name from 1981. It is not cshw, and cshw does not provide a print command, because the Unix world never had one either. What it had, and what cshw now has, is the lp family.
Unix ended up with two printing command sets, for the usual reasons:
lp to submit, lpstat to inspect, cancel to remove.lpr to submit, lpq to inspect, lprm to remove.They do the same things with different names and gratuitously different flags. cshw provides all of them, and — this is the useful part — they are the same commands underneath. lp and lpr both accept -d and -P for the destination. lprm and cancel are one command with two spellings. A script ported from either tradition just runs.
lp -d "Front Desk" report.txt # System V habits
lpr -P "Front Desk" report.txt # BSD habits
This is the part worth understanding, because getting it wrong wastes paper rather than raising an error.
lp report.ps
The bytes go to the printer untouched. That is exactly right when the file is already in a language the printer speaks — PostScript, PCL — and it's the default because it's the only mode that doesn't touch your data.
It's also the mode that fails most confusingly. A printer will accept RAW quite happily and then print forty pages of gibberish, because acceptance happens in the spooler and interpretation happens in the device. Those are different places.
--text — let the spooler render itlp --text notes.txt
The spooler's print processor turns plain text into whatever the device needs. No fonts to pick, no margins to set.
When it works, it's the easiest thing in the chapter. It doesn't always work: a printer driver can refuse the TEXT datatype even when its print processor advertises it. On a stock Windows 11 machine, Print to PDF, the XPS writer and OneNote all refuse it; the Fax driver accepts it. cshw tells you plainly when that happens, and what to use instead.
--gdi — cshw renders itlp --gdi --font "Courier New" --size 9 --margin 1.0 notes.txt
cshw lays the text out itself: font, size, margins, orientation, pagination. Works on any printer, because by the time the driver sees it, it's a rendered page like any other.
--font NAME |
default Consolas |
--size POINTS |
default 10 |
--margin INCHES |
default 0.5 |
--landscape |
rotate the page |
You may be wondering why the shell doesn't just work out which mode to use.
Because it can't. There is no Windows API that answers "does this device understand PostScript?" You can ask what the spooler accepts, and you can ask what the driver prefers, and neither of those is the question. A guess that's wrong doesn't produce an error — it produces a ream of garbage and a puzzled look.
So cshw reports, and you decide:
lpstat -l
printer Microsoft Print to PDF
driver: Microsoft Print To PDF
port: PORTPROMPT:
processor:winprint
datatype: RAW
jobs: 0
accepts: RAW, RAW [FF appended], RAW [FF auto], NT EMF 1.003,
NT EMF 1.006, NT EMF 1.007, NT EMF 1.008, TEXT, XPS2GDI
TECHNICAL STUFF
That accepts line is the print processor's list, and it is necessary but not sufficient. The driver gets a veto. This is exactly why the line is reported rather than acted on: if cshw treated it as authoritative, it would confidently pick TEXT for a printer that rejects TEXT.
Three targets write a file instead of putting ink on paper. All three work with no printer attached, and none of them raises a Save-As dialog.
lp --pdf out.pdf report.txt
lp --xps out.xps report.txt
lp --image out.tif report.txt
The first two go through real Windows printers — Microsoft Print to PDF and Microsoft XPS Document Writer, both present on every modern Windows install. Those printers normally pop a Save-As dialog; naming the output file suppresses it, which is what makes them usable from a script.
The third is different, and worth understanding: there is no "Print to TIFF" printer on Windows. The old Office Document Image Writer is long gone. So --image doesn't use a printer at all — cshw renders the pages itself, with the same layout engine --gdi uses, and encodes them directly.
That makes --image the most predictable of the three: no driver, no spooler, no queue, no dialog, nothing to be installed. It's also why cshw's own test suite leans on it.
lp --image scan.tif --imagetype tiff --dpi 300 report.txt
--imagetype |
tiff (default), png, jpg, bmp |
--dpi N |
default 200 |
--tiff-compression |
g4 (default), lzw, none |
The defaults are not arbitrary. A letter page of text, measured:
| DPI | TIFF‑G4 | PNG | JPG | BMP |
|---|---|---|---|---|
| 96 | 12 KB | 58 KB | 153 KB | 3.4 MB |
| 200 | 34 KB | 344 KB | 964 KB | 15 MB |
| 300 | 50 KB | 435 KB | 1.4 MB | 34 MB |
TIFF with G4 compression wins by an enormous margin for text, because G4 is a bilevel codec — one bit per pixel, black or white — designed for exactly this. At 200 dpi the same page is 15 KB as G4, 96 KB as LZW, and 14.9 MB uncompressed.
⚠️ JPEG is the wrong choice for text. It is both larger than TIFF and lossy, and the artifacts land on precisely the sharp black-on-white edges that letters are made of. It's supported, and it's fine for a page with photographs on it. For a page of words, use TIFF.
TIFF holds every page of a document in a single file. PNG, JPG and BMP cannot — they're single-image formats. So for those, cshw writes one file per page:
lp --image report.png --imagetype png report.txt
report-pg1.png
report-pg2.png
report-pg3.png
and prints each name as it goes, so a script can capture the list. Page numbers are zero-padded to the width of the largest, so a twelve-page document gives report-pg01.png through report-pg12.png and still sorts correctly in a foreach.
TECHNICAL STUFF
Every page gets a -pgN, including page 1 of a one-page document. That looks like fussiness until you consider the alternative: if the suffix appeared only for multi-page documents, the output filename would depend on the document's content. A script that wrote out.png and then read out.png would work perfectly until somebody's report grew a second page.
lpq
Microsoft Print to PDF:
Job Owner Status Size Document
17 tarie spooling 20480 report.txt
That job id is what the removal commands take:
lprm 17 # BSD spelling
cancel 17 # System V spelling -- same command
lprm -a # everything in the queue
And lpc controls the queue itself:
lpc status # is it accepting work? how many entries?
lpc stop # pause -- jobs pile up, nothing prints
lpc start # resume
lpc purge # discard everything queued
⚠️
stop,startandpurgechange state shared with everyone else using that printer, so they need the rights to manage it and fail with access denied without them. cshw never elevates itself — run an elevated shell, or usesudo. Andpurgeis not undoable; runlpqfirst if you want to know what you're throwing away.
Chapter 6 gave you fmt, pr and fold for shaping text. This is what they were shaping it for:
fmt -w 65 letter.txt | pr -h "Draft" | lp --pdf draft.pdf
Reflow the prose to 65 columns, paginate it with a title and page numbers, render it to a PDF. No printer required, no dialog, and every stage is a tool you already know.
A nightly report, in full:
#!/usr/bin/env cshw
# Yesterday's errors, formatted and archived as a PDF.
set stamp = `say datetime /fmt:"%Y-%m-%d"`
grep ERROR /logs/app.log \
| fmt -w 78 \
| pr -h "Error report $stamp" \
| lp --pdf "/reports/errors-$stamp.pdf"
echo "wrote /reports/errors-$stamp.pdf"
You can now put text on paper, or into a PDF, or into a stack of images, without leaving the shell and without a printer being involved at all.
That's the last of the feature chapters. What follows is the Part of Tens: Chapter 21 collects the commands you'll reach for daily, Chapter 22 is the list of things cshw does that cmd.exe simply can't, and Chapter 23 is the one to read when something has gone wrong and you need to make progress.