Is silence output in a bash script using a function recommended?

Suppose someone writes a bash script in which to disable stdout, stderr and provide user output.

Is it possible to use the function as shown below:

dump(){
    "$@" > /dev/null 2>&1
}

And then

dump rm filename || echo "custom-message"

What are the possible cases when it does not work properly?

+4
source share
1 answer

This is a good technique. I use something like this all the time. Pros:

  • Saves the command exit code.
  • Hides the output of almost every program, unless they write directly to /dev/ttyor /dev/console, which is rare and probably for a good reason.
  • , . cd, pushd/popd ..
  • stdin. dump , .
  • "$@" , .

!

, , , dump .

+6

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


All Articles