Issue
printf "line one\n"
printf "line two\n"
After these printouts I want to replace line one and print something else
(without using clear
). I've tried commands like:
printf "line one\n"
printf "line two\r"
It's not what I want because it replaces the last line line two
, not line one
.
What I want to do:
printf "line one\n"
printf "line two\n"
sleep 0.5
somecode "line three"
Output I want:
line three
line two
Solution
you can move the cursor in bash scripts via printing special escape sequences, try this code:
#!/bin/bash
# print first line
printf "first line is long\n"
# print second line
printf "line 2\n"
sleep 1
# move cursor two steps UP
printf "\033[2A"
# print line 3 (without \n)
printf "line #3"
# clear rest of first line
# and move cursor two steps down
printf "\033[K\r\033[2B"
more about ANSI Escape Sequences: https://tldp.org/HOWTO/Bash-Prompt-HOWTO/c327.html
Answered By - Игорь Тыра