0

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?

spraff
  • 32,570
  • 22
  • 121
  • 229
  • 2
    there's no "broken pipe" here. do you mean the `... line 17: 23327 Killed ...` message? – pynexj Mar 24 '21 at 09:44
  • 1
    That's not a broken pipe error, it's the shell's job control feature reporting that a process it was managing has been killed. To remove it from job control, add `disown $PID` before killing it. – Gordon Davisson Mar 24 '21 at 09:45
  • you can also do `{ kill -9 $PID && wait $PID; } &> /dev/null ` – pyr0 Mar 25 '21 at 14:00

0 Answers0