Trouble Sending Printer Codes from Perl to Printer

Eric Blake ericblake@comcast.net
Fri Jul 1 00:13:00 GMT 2005


> <BASH TRANSCRIPT>
> $ echo -en "hello\nworld"
> hello
> world
> dvergin@GatewayM275 /c/bin
> $     #Good! The basic approach works on the command line.
>        #Control codes are interpreted and no trailing newline.
>        #So I'll try the same thing from perl letting the echo
>        # command convert the control char.
>        #   (Note different approach farther down)
> 
> $ perl -e 'system(q/echo -en "hello\nworld"/)'
> -en hello\nworld
> 
> dvergin@GatewayM275 /c/bin
> $     #Ack! The -en option is treated as text to be echoed.

Ah - the classic /bin/sh is not /bin/bash.  system() uses /bin/sh, and ash
unfortunately doesn't recognize -en as an argument to echo.  POSIX
recommends using printf, not echo, when you want more control over
what system() outputs:

$ perl -e 'system(q/printf "hello\0\nworld"/)' | od -tx1
0000000 68 65 6c 6c 6f 00 0a 77 6f 72 6c 64
0000014

Aha - \0 worked, \n was interpreted, and no trailing newline.

And by the way, this is not cygwin specific - any use of \ escape sequences
as an argument to echo is inherently non-portable across different shells.

--
Eric Blake



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list