16

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?

2
  • 3
    watch is not a builtin. It is an external command.
    – muru
    Feb 6, 2016 at 10:46
  • 1
    Something mangle your $PATH?
    – Jeff Schaller
    Feb 6, 2016 at 11:07

2 Answers 2

24

watch is not an internal command:

$ type watch
/usr/bin/watch

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

1
1

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
}
2
  • 1
    Why reinvent the wheel? Also, how about the other options like -d, -g, etc?
    – GMaster
    May 16, 2020 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, 2020 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.