I have the following script, which should read the output from another command and then wait for changes, syncing these to a target directory. I replaced the actual commands with sleep and cat to make it easier to reproduce
cat .dirs.txt | while read dir
do
echo "Watching files in $dir"
while true
do
sleep 10
echo "Detected change in $dir"
done &
done
jobs
wait
I see the expected "Watching files in XY" output, but the jobs command shows no jobs, and wait immediately continues. I do see the "Detected change in XY" every 10 seconds, so the jobs are actually executing in the background. I suspect it has something to do with the read command and the pipe, because it does work with a redirect instead of a pipe. So why is this not working as I expected?