29

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
  • 4
    watch is not a builtin. It is an external command. Commented Feb 6, 2016 at 10:46
  • 1
    Something mangle your $PATH? Commented Feb 6, 2016 at 11:07

3 Answers 3

37

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
  • 40
    That was it. I'm running Mac OSX, which didn't come with it. So installed via homebrew: brew install watch. stackoverflow.com/a/23370705/1175956 Commented Feb 6, 2016 at 11:45
3

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? Commented 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. Commented May 16, 2020 at 15:49
1

As pointed out by @Han, if you use that script that he made you will also be able to run commands using your local aliases, and also preserve terminal colors.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.