zsh-users
 help / color / mirror / code / Atom feed
* printf newline
@ 2016-11-06 17:29 ` Johan DS
  2016-11-07  9:46   ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Johan DS @ 2016-11-06 17:29 UTC (permalink / raw)
  To: Zsh-Users List

[-- Attachment #1: Type: text/plain, Size: 115 bytes --]

hi
does anybody has a clou why if I:
$ printf texta \n textb

The output is:

texta%

and not:
texta
textb

Thanks

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: printf newline
  2016-11-06 17:29 ` printf newline Johan DS
@ 2016-11-07  9:46   ` Peter Stephenson
  2016-11-07 16:51     ` Daniel Shahaf
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Stephenson @ 2016-11-07  9:46 UTC (permalink / raw)
  To: Zsh-Users List

On Sun, 6 Nov 2016 18:29:44 +0100
Johan DS <victor3xray@gmail.com> 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.)

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: printf newline
  2016-11-07  9:46   ` Peter Stephenson
@ 2016-11-07 16:51     ` Daniel Shahaf
  0 siblings, 0 replies; 3+ messages in thread
From: Daniel Shahaf @ 2016-11-07 16:51 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh-Users List

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@gmail.com> 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
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-11-07 17:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20161107082025epcas3p2d5ebaf5a3e7b4f9e3373715666e940d9@epcas3p2.samsung.com>
2016-11-06 17:29 ` printf newline Johan DS
2016-11-07  9:46   ` Peter Stephenson
2016-11-07 16:51     ` Daniel Shahaf

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).