MacOS – How to Send Text (and Not a Text File) to My Printer on OS X

bashmacosprintingterminal

I know about this question (How do I send ASCII text to printer (over USB)?). But I'm working on something different. I'd like to be able to send text from within a bash script or Perl script to my printer.

Everything I find tells me about sending a text file to my printer. Is there any way to send text from within a script? (That way I don't have to generate the text, save it as a file, then print the file, then delete the file.)

Best Answer

lp is able to read data from standard input, see man lp:

lp submits files for printing or alters a pending job. Use a filename of "-" to force printing from the standard input.

So if you followed the advice given in the referenced question to install a "raw" printer you should be able to run

echo "whatever text you want to print" | lp -d Generic_usbtb -