I have a kubernetes command kubectl get pods
which prints to the terminal this output
NAME READY STATUS RESTARTS AGE
redis-cart-74594bd569-ffrhk 1/1 Running 0 40m
I want this command to run repeatedly every two seconds in a bash script.
However, I want the output to overwrite the last input.
Currently, I can get it to overwrite one line (the first outputted line) by using a carriage return. But it doesn’t overwrite the second line.
How can I overwrite previously outputted text from my bash script?
By script looks like this atm
#!/bin/bash
variable=$(kubectl get pods)
while true
do
variable=$(kubectl get pods)
echo -ne "$variable" "r"
sleep 2
done
exit
2
Answers
Add
clear
command to the loop:But, you can use
watch
or-w
to get a similar result without a special script.try
watch
to refresh output …To end the process you can use
CTRL+C
or look for the process viaps
orpgrep
and send a SIGINT likepgrep watch kubectl | xargs kill -1
.