0

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
  • 1
    I don't understand why you want to do that if you have available commands such us watch. Commented Oct 12, 2021 at 10:39
  • I didn't realise the watch commands existence, looking into it now Commented Oct 12, 2021 at 10:41

2 Answers 2

2

Add clear command to the loop:

#!/bin/bash variable=$(kubectl get pods) while true do variable=$(kubectl get pods) clear echo -ne "$variable" "\r" sleep 2 done 

But, you can use watch or -w to get a similar result without a special script.

Sign up to request clarification or add additional context in comments.

Comments

1

try watchto refresh output ...

watch kubectl get pods 

To end the process you can use CTRL+C or look for the process via ps or pgrepand send a SIGINT like pgrep watch kubectl | xargs kill -1.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.