I have multiple piped commands, like this:
find [options] | grep [options] | xargs grep [options]
Each one of them can potentially produce errors (permissions errors, spaces-in-filenames errors, etc) that I am not interested in. So, I want to redirect all errors to /dev/null. I know I can do this with 2>/dev/null, for each command. Can I set IO redirection persistently? Ideally, I would just set it once, at the beginning/end of the command, and then it would affect all subsequent/preceding piped commands. Also, can IO redirection be set permanently, so that it continues to affect all commands until it is reset?
I’m using bash (I checked the man page for bash builtins and did not see the ‘>’ and ‘<‘ characters at the top, so I assumed it was a linux thing… sorry)
I’m going to assume that you’re using bash, or at least some sort of Bourne-like shell.
I’m also assuming that what you want to avoid is the following:
i.e. repeating the
2>/dev/nullpart for each segment of the pipeline.You can do that with:
You can also set the redirection permanently as follows:
and (assuming that STDOUT and STDERR were both pointing to the same place before), you can undo that with: