Prevent "broken pipe" for child process in bash

619 Views Asked by At

This has a broken pipe error which I want to suppress:

#!/bin/bash

touch /tmp/foo

tail -f /tmp/foo &

PID=$!

echo before
kill -9 $PID
echo after

while true;
do
    echo x
    sleep 1
done

output:

before
after
x
./test.sh: line 17: 23327 Killed                  tail -f /tmp/foo
x
x
^C

I tried trap -- '' PIPE and subshells and bash -c "tail -f /tmp/foo" 2>/dev/null & but the message persists.

How do I suppress it?

0

There are 0 best solutions below