Question

I'm sure I'm missing something simple, but I'm using an executive script to call a few utility scripts and I want to handle all of the output from the utilities via one pipe. My issue is the utilities use stderr to report error conditions, but I can't capture that for use in the parent script.

Parent Script:

#!/bin/bash
child 2>&1 >/dev/null

Child Script

#!/bin/bash
echo "Print"
echo "Error" 1>&2

What I expect is that the stderr of child (and all of it's commands) is redirected to stdout (hence no output), but when I execute parent I get Error echo'd to the terminal ("Print" is sent to /dev/null).

Was it helpful?

Solution

What's happening is that you're redirecting stderr to stdout's filehandle (perhaps /dev/stdout). Then you redirect stdout to /dev/null. However, stderr is still pointing to /dev/stdout.

To ignore all output, first redirect stdout, then redirect stderr to stdout

child >/dev/null 2>&1

Or, more simply since you're using bash specifically, redirect stdout and stderr together:

child &>/dev/null

OTHER TIPS

Not an answer to the OP's question, but in case someone else is struggling to figure out how to redirect a child process's output to the parent's stdout, here it is.

#!/bin/bash
child &>>/dev/stdout &

This does the trick and will even work when backgrounding the child task as shown.

Thanks go to @glenn jackman for being the first person in a day-long search for a solution to mention /dev/stdout in his answer.

Update

Upon further research, with this answer being particularly helpful, it seems there's a way of doing this that also preserves the parent's stderr:

#!/bin/bash
child 1>&1 2>&2 &
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top