24

In bash, watch (e.g. watch -n 5 ls -l) could be used to repeat the command at fixed intervals.

This command seem to be missing on zsh. Is there an equivalent?

Anthon
  • 79,293
snowbound
  • 343

2 Answers2

32

watch is not an internal command:

$ type watch
/usr/bin/watch

so make sure it installed on the system where you are running zsh.

Anthon
  • 79,293
  • 34
    That was it. I'm running Mac OSX, which didn't come with it. So installed via homebrew: brew install watch. http://stackoverflow.com/a/23370705/1175956 – snowbound Feb 06 '16 at 11:45
2

I just improvised watch with: clear; while ls -lah; do sleep 2; clear; done

Interesting idea. I could write a function around that... Right, put this in your .zshrc

watch () {
    IN=2
    case $1 in
        -n)
            IN=$2
            shift 2
            ;;
    esac
    printf '\033c' # clear
    CM="$*"
    LEFT="$(printf 'Every %.1f: %s' $IN $CM)"
    ((PAD = COLUMNS - ${#LEFT}))
    while :
    do
        DT=$(date)
        printf "$LEFT%${PAD}s\n" "$HOST $(date)"
        eval "$CM"
        sleep $IN
        printf '\033c'
    done
}
Han
  • 66
  • 1
    Why reinvent the wheel? Also, how about the other options like -d, -g, etc? – GMaster May 16 '20 at 15:45
  • 2
    Because not everybody uses Linux where it is installed by default. On OSX and the BSDs you'd have to install a package. Also, my aliases won't work in the normal watch. I don't care about the other features. If you do, feel free to add them. Also, you learn coding by doing it. – Han May 16 '20 at 15:49