Search code examples
shellzsh

macOS zsh prints newline at end


The following justifies the "[COW]" to the right, but also adds a newline. I want to not have a newline so that the next thing printed is printed on the same line (but left-justified). How do I do this? All the docs I've seen say printf doesn't add a newline but if you double or triple the 2nd line below, it's obvious that it does

col=$(tput cols)
printf '%*s'  $col "[COW]"
printf "do you see that %s over there:" "cow"

NOTE: zsh 5.8 (x86_64-apple-darwin20.0)


Solution

  • printf isn't adding a newline. The terminal emulator is scrolling because you wrote past the end of the line.

    You should write a carriage return at the end of the first message, so it goes back to the beginning of the line.

    col=$(tput cols)
    printf '%*s\r'  $col "[COW]"
    printf "do you see that %s over there:" "cow"