Last active
September 15, 2025 15:55
-
-
Save espaciomore/28e24ce4f91177c0964f4f67bb5c5fda to your computer and use it in GitHub Desktop.
Watch command for Git Bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| ARGS="${@}" | |
| clear; | |
| while(true); do | |
| OUTPUT=`$ARGS` | |
| clear | |
| echo -e "${OUTPUT[@]}" | |
| done |
And I added some additional information to the watch function to make it look like it does in native linux -
watch () { ARGS="${@}" clear; while(true); do OUTPUT=`$ARGS` clear echo -e "Every 1.0s: $ARGS" echo "" echo -e "${OUTPUT[@]}" sleep 1 done }
this is awesome!!!
I added the -n parameter support. For example watch -n 5 ls -l
watch() {
local interval=1 # Standardintervall
local command_to_run=""
# Einfache Argumenten-Analyse
if [[ "$1" == "-n" && "$2" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
interval="$2"
shift 2 # Entfernt -n und die Zahl von den Argumenten
fi
command_to_run="${@}"
if [ -z "$command_to_run" ]; then
echo "Usage: watch [-n <seconds>] <command>"
return 1
fi
clear
while true; do
# Führe den Befehl aus und speichere Ausgabe und Fehler
OUTPUT="$($command_to_run 2>&1)"
clear
echo -e "Every ${interval}s: $command_to_run"
echo ""
echo -e "${OUTPUT}"
sleep "$interval"
done
}Additional update on top of @ManuelReschke's version! I've added the eval command to support the usage of | and other more complex commands
watch() {
local interval=1 # Standardintervall
local command_to_run=""
# Einfache Argumenten-Analyse
if [[ "$1" == "-n" && "$2" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
interval="$2"
shift 2 # Entfernt -n und die Zahl von den Argumenten
fi
command_to_run="${@}"
if [ -z "$command_to_run" ]; then
echo "Usage: watch [-n <seconds>] <command>"
return 1
fi
clear
while true; do
# Führe den Befehl aus und speichere Ausgabe und Fehler
OUTPUT="$( eval "$command_to_run" 2>&1)"
clear
echo -e "Every ${interval}s: $command_to_run"
echo ""
echo -e "${OUTPUT}"
sleep "$interval"
done
}verified in Git Bash terminal
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And I added some additional information to the watch function to make it look like it does in native linux -