Function 如何在zsh中使用带功能的手表?

Function 如何在zsh中使用带功能的手表?,function,zsh,watch,Function,Zsh,Watch,我想在watch调用中使用自定义函数。下面是一个虚拟示例: my_fun(){ head -c2 } echo hello | my_fun # he watch 'echo hello | my_fun' # my_fun: command not found 如何让watch了解my_fun?watch实用程序使用sh-c执行命令。因此,如果要执行zsh函数,需要显式执行zsh。例如,如果在/path/to/functions文件中定义了函数,则可以执行以下操作: watch 'e

我想在
watch
调用中使用自定义函数。下面是一个虚拟示例:

my_fun(){
    head -c2
}
echo hello | my_fun
# he
watch 'echo hello | my_fun'
# my_fun: command not found

如何让
watch
了解
my_fun

watch
实用程序使用
sh-c
执行命令。因此,如果要执行zsh函数,需要显式执行zsh。例如,如果在
/path/to/functions
文件中定义了函数,则可以执行以下操作:

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


但是,编写包含所有内容的脚本可能比编写zsh函数要好。

太棒了!我选择了第二种选择。谢谢
watch 'zsh -c "source /path/to/functions; echo hello | my_fun"'