Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: printf newline



Peter Stephenson wrote on Mon, Nov 07, 2016 at 09:46:29 +0000:
> On Sun, 6 Nov 2016 18:29:44 +0100
> Johan DS <victor3xray@xxxxxxxxx> wrote:
> > hi
> > does anybody has a clou why if I:
> > $ printf texta \n textb
> > 
> > The output is:
> > 
> > texta%
> > 
> > and not:
> > texta
> > textb
> 
> (I presume you've missed out some quotes?  In what you show the
> backslash would be swallowed up when reading the command line and so
> you'd get an "n" rather than a newline.)
> 

I think the question was, why the \n and the textb weren't used.

The answer is that printf's interface is different from echo's.  printf
interprets its first argument as a format string that describes how to
use the remaining arguments.  The format string "texta" instructs to
print "texta" and to ignore any further arguments.  The "%" is then
generated by the PROMPT_* options.

For the \n to be interpreted by printf, it must be in the first
argument.  Adding the details Peter described gives:

    printf 'texta\ntextb\n'

which would do what you want.

Cheers,

Daniel

> There's no implicit newline at the end of printf output, unlike print.
> Because the shell needs to know where it is on the terminal (as zsh
> handles multiple lines on the screen, unlike most other command line
> environments), it goes back to the beginning of the line, swallowing up
> the textb.
> 
> If you really need, you can "unsetopt prompt_cr", but you're liable to
> see side effects.  For most of us it's usually more convenient just to
> add the extra newlines.
> 
> pws
> 



Messages sorted by: Reverse Date, Date, Thread, Author