To update the line the cursor is on, send a CR ("carriage return", \r
) to send ("return") the cursor to the beginning of the existing line, from which you can print new contents. Contrast this to the newline (\n
), which moves the cursor to a new line.
To see this in action, try running the following:
printf '%s\r' "Existing contents being written here"
sleep 1
printf '%s\r' "New contents being written here "
sleep 1
printf '%s\n' "Writing final contents and moving to a new line"
printf '%s\n' "This is written to a second line."
Note how the second line has some extra whitespace on the end; this padding is there to make sure that the end of the original line's contents are overwritten.
That said, if you just want a status bar already built for you, there are numerous solutions for that already: