Is there any way to make the linux CLI IO redirect permanent?

I have several commands with channels, for example:

find [options] | grep [options] | xargs grep [options]

Each of them can potentially create errors (permission errors, space errors in file names, etc.) that do not interest me. So I want to redirect all errors to / dev / null. I know that I can do this with help 2>/dev/null, for each team . Can I reassign I / O redirection? Ideally, I would just install it once, at the beginning / end of the command, and then it would affect all subsequent / previous commands. Alternatively, can I redirect the I / O redirection so that it continues to affect all commands until it is reset?

I use bash (I checked the man page for bash built-in functions and did not see the '>' and '<' characters at the top, so I assumed it was a Linux thing ... sorry)

+3
source share
2 answers

I am going to suggest that you are using bash or at least some kind of shell similar to Bourne.

I also assume that you want to avoid:

find ... 2>/dev/null | grep ... 2>/dev/null | xargs ... 2>/dev/null

i.e. repeating part 2>/dev/nullfor each conveyor segment.

You can do it with

( find ... | grep ... | xargs ... ) 2>/dev/null

You can also set the redirection permanently:

exec 2>/dev/null

and (assuming that STDOUT and STDERR were both pointing to the same place before), you can undo this with:

exec 2>&1
+7
source

stderr ?

-

$ bash 2>/dev/null -c "find [options] | grep [options] | xargs grep [options]"

, (.. ),

$ bash 2>/dev/null -c "echo nope 1>&2"

, , , - , .

0

Source: https://habr.com/ru/post/1745155/


All Articles