<> == I have a pipeline where a long-running command feeds into a filter. If the filter finds "foo", I want the long-running command to die. == In general this is not possible, because sibling processes (two children of the same parent) do not have any knowledge of each other. But consider the following example and answers: We'll assume the following pipeline, which uses Linux's [[https://manpages.debian.org/inotifywait|inotifywait]] program: {{{#!highlight bash inotifywait -m --format '%e %f' uploads | while read -r events file; do if [[ $events = *MOVED_TO* ]]; then : do something special if [[ $file = *abort* ]]; then : special sentinel file found : want to kill the inotifywait process fi fi done }}} In this example, the user wants to stop the `inotifywait` program if a rename is performed with the resulting filename containing the string "abort". What are our options here? * Simply `exit` from the `while` loop, and do not explicitly try to do anything to `inotifywait`. The shell process containing the `while` loop will terminate, and the `inotifywait` process will continue running. As soon as `inotifywait` tries to write another line of output to the pipe (which has been closed), it will receive a `SIGPIPE` signal, which will terminate it. * Find an option within the feeder process to exit when something bad happens, instead of relying on an external filter to do it for you. (This will only be possible with certain feeder programs, but it's worth taking the time to ''read the documentation'' and see whether it's possible.) * Replace the anonymous pipeline with a [[NamedPipes|FIFO]] and run the feeder as a background process explicitly. Save its PID. Wait for the filter to exit, and when it does so, kill the long-running feeder process. Thus: . {{{#!highlight bash filter() { while read -r events file; do : ... done } fifo="${TMPDIR:-${XDG_RUNTIME_DIR:-/tmp}}//fifo.$$" trap 'rm -rf -- "$fifo"' EXIT mkfifo -- "$fifo" || exit inotifywait -m --format '%e %f' uploads > "$fifo" & pid=$! filter < "$fifo" kill -- "$pid" wait }}} . A process substitution can be used instead of a fifo {{{#!highlight bash { filter ; } < <( inotifywait -m --format '%e %f' uploads ) kill "$!" # kill inotifywait as soon as filter returns wait "$!" # wait for inotifywait to reap its exit status. (bash 4.4+) }}}