Search code examples
bashjobsabort

Suppress just bash job control messages in scripts (Aborted, Broken pipe)


I have the following setup:

aborter.cpp

#include <cassert>
int main(int argc, char** argv) { assert(argc >= 2); return 0; }
// compiled to 'aborter'

test.sh

#!/usr/bin/bash

function runpipe() {
    yes | aborter "$@"
    test $? -ne 0 || echo OK
}
runpipe "$@"

Running test.sh 123 prints only OK. Running just test.sh outputs the following:

aborter: aborter.cpp:2: int main(int, char**): Assertion `argc >= 2' failed.
./test.sh: line 3:  2521 Broken pipe             yes
      2522 Aborted                 | aborter "$@"

How do I suppress the 'Broken pipe' and 'Aborted' job control lines output by bash in the second case? I want the output to be just the C++ assert dump (the first line).

This should not require editing aborter.cpp or the test.sh interface.


Solution

  • How do I suppress the 'Broken pipe' and 'Aborted' job control lines output by bash in the second case?

    Redirect bash stderr to null except for you process.

    exec 3<&2 2>/dev/null
    function runpipe() {
        yes | aborter "$@" 2>&3
        test $? -ne 0 || echo OK
    }
    runpipe "$@"