在 shell 函数中睡眠 inotifywait 不工作

sleep inside inotifywait in a shell function not working

我正在尝试运行以下功能

foo () {
    sleep 1
    echo "outside inotify"
    (inotifywait . -e create |
    while read path action file; do
        echo "test"
        sleep 1
    done)
    echo "end"
}

直到 inotify 正确等待 运行s;我看到了:

>> foo
outside inotify
Setting up watches.
Watches established.

然而,一旦我创建了一个文件,我就得到

>>> fooo
outside inotify
Setting up watches.
Watches established.
test
foo:6: command not found: sleep
end

知道为什么吗?另外,我是否需要围绕 inotifywait 生成子进程 ()?有什么好处?

谢谢。

编辑 我意识到我正在 运行ning 上 zsh

read path 把你搞砸了,因为与 POSIX 兼容的 shells 不同——它保证只有修改名称全大写的变量才会产生不需要的副作用在 shell 本身——zsh 也有几个小写名称的特殊行为,包括 path.

特别是,zsh 将 path 呈现为与 PATH 中的值相对应的数组。为该数组分配一个字符串也会覆盖您的 PATH