How to use watch with a function in zsh?

function, watch, zsh

Solution

The `watch` utility executes commands with `sh -c`. So, if you want to execute a zsh function, you need to execute zsh explicitly. For instance, if your functions are defined in a `/path/to/functions` file, you can do:

watch 'echo hello | zsh -c "source /path/to/functions; my_fun"'

or

watch 'zsh -c "source /path/to/functions; echo hello | my_fun"'

But it is probably better to write a script with everything in it than a zsh function.

Problem

I want to use a custom function inside a `watch` call. Here's a dummy example: ``` my_fun(){ head -c2 } echo hello | my_fun # he watch 'echo hello | my_fun' # my_fun: command not found ``` How do I let `watch` know about `my_fun`?

Original source