I have a command say printf "%5s1" and its output is:


How can I print digit 2 to left and digit 3 to right of already printed 1. So, the output looks like below:


So, there is one condition here that 1 is already printed using command printf "%5s1".

I've tried using below command,

 printf "%5s1";printf "\r%4s2%1s3\n"

but this overwrites the existing output 1 and I get below output.

   2 3

How can I print a digit right side of already printed digit 1 using printf without loosing it assuming that I have to use printf "%5s1" first? I'm looking for something like:

printf "%5s1";printf "\r%4s2???\n"
  • What are you asking? Is it me or are others completely lost on this question?
    – slm
    Aug 29, 2013 at 19:53
  • @slm The way I read it, the OP wants to print a string to the left of another string already printed and another string to the right of the original string. For some reason, the middle string is printed first in a separate statement, which is why the OP is using a carriage return to print to its left.
    – Joseph R.
    Aug 29, 2013 at 19:57
  • @JosephR. - thanks, I started reading about the \r and the tput and then put 2 and 2 together and realized what he/she was asking.
    – slm
    Aug 29, 2013 at 19:58
  • @slm its he BTW.
    – g4ur4v
    Aug 30, 2013 at 5:48
  • @g4ur4v - sorry, hopefully I didn't offend with that. I wasn't sure and was keeping it generic.
    – slm
    Aug 30, 2013 at 5:50

3 Answers 3

left=$(tput cub1) right=$(tput cuf1)
printf %5s1; printf '%s\n' "$left${left}2${right}3"


From the Bash Prompt HOWTO:

tput cub1
move left one space

tput cuf1
non-destructive space (move right one space)

Simply reverse the order you're printing in and print the string you need on the right first:

printf "%5s1";printf "3\r%4s2\n"

Also, use \b to backup:

$ printf "%5s1";printf "3\b\b\b2\n"
  • 2
    May be better to use $(tput cub1) instead of hardcoding \b for those terminals where \b is destructive and where cub1 is something else (probably rare nowadays though) Aug 29, 2013 at 20:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .