Make a bash script that refresh values without scrolling down the terminal like TOP command

时间:2019-04-16 22:45:14

标签: linux bash shell

How to set a bash script to refresh the screen with variable values like TOP command?

To show the variable value I use:

echo "$var"

But it prints a new line, instead of I would like something that refresh the screen

enter image description here

How to achieve this?

1 个答案:

答案 0 :(得分:2)

The easiest way is to just use watch:

watch date

You can also just call clear before each iteration. Here, all the output from clear and any commands are collected as a form of double buffering to reduce flickering:

show_things() {
  date
  uname -a
  echo "Your lucky number is $RANDOM"
}

while sleep 1
do
  printf '%s\n' "$(clear; show_things)"
done