Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Watch command for Git Bash
#!/bin/bash
ARGS="${@}"
clear;
while(true); do
OUTPUT=`$ARGS`
clear
echo -e "${OUTPUT[@]}"
done
@FabianTe
Copy link

FabianTe commented Nov 6, 2018

Do you save that file in a folder that is included in the PATH?

@amnonkhen
Copy link

amnonkhen commented Feb 11, 2019

@FabianTe, yes

@Opimenov
Copy link

Opimenov commented May 9, 2019

I'd add sleep 1 after echo, so the console does not flicker.

@8ctopus
Copy link

8ctopus commented Jan 15, 2021

thanks did the trick!

@mchandschuh
Copy link

mchandschuh commented Mar 2, 2021

I found this to be slightly better at getting rid of most of the flicker:

#!/bin/bash
ARGS="${@}"
clear;
while(true); do
  clear
  OUTPUT=`$ARGS`
  echo -e "${OUTPUT[@]}"
  sleep 1
done

@saadzimat430
Copy link

saadzimat430 commented Mar 30, 2021

@mchandschuh for Windows users, in which folder should I paste the .sh script?

@8ctopus
Copy link

8ctopus commented Mar 31, 2021

@saadzimat430 C:\Users\{user}\.bashrc

@keydon
Copy link

keydon commented Sep 7, 2021

I tweaked @mchandschuh version a little more by executing the command before clearing the screen. Espacially for long-running commands there is no more flickering for me.

#!/bin/bash
ARGS="${@}"
clear;
while(true); do
  OUTPUT=`$ARGS`
  clear
  echo -e "${OUTPUT[@]}"
  sleep 1
done

@TGillispie
Copy link

TGillispie commented Sep 23, 2021

Thanks for this and all the improvements!

I made a version that is nearly flicker-less. It clears the previous 25 lines. Any lines of output beyond 25 will scroll.

#!/bin/bash
ARGS="${@}"
clear;
while(true); do
  OUTPUT=`$ARGS`
  for i in {1..25}; do
    printf '\e[1A\e[K'
  done

  echo -e "${OUTPUT[@]}"
  sleep 1
done

@hkskoglund
Copy link

hkskoglund commented Nov 6, 2021

mingw64 does not have watch so I found this gist. I tried to look at the esacpe sequence of original watch with strace with strace -f watch echo test. Found this escape sequence: write(1, "\33[1;75H", 7�[1;75H) = 7

#!/usr/bin/bash
interval=2
if [ "$1" = "-n" ]; then # allow -n {interval} at start 
  interval=$2
  shift 2
fi
clear
while true; do
   printf "%b\n\n" "\033[1;1HEvery $interval s:$*\033[1;75H$(hostname): $(date)"
   "$@"
   sleep "$interval" 
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment