0

I am working with shell scripts for some automation tasks. I have functions defined in my script which are executed however the tasks performed by some of the functions logs a lot of data on the terminal screen which is irrelevant.

Is there a way in which I can suppress the console logging before calling a particular function and enable it back when the function execution is finished?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Write the functions to stdout, stderr or tty? – Cyrus May 15 '22 at 22:11
  • could you elaborate please. – Satish Kumar Singh May 15 '22 at 22:46
  • typically there are a few output streams for a program (no necessarily a shell script), named respectively standard output (`stdout`), standard error (`stderr`). Sometimes a standard log (`stdlog`) is added to the list, and a program, can access terminal device directly via `tty`. The functions write their output to one of those streams. You may check examples here: https://stackoverflow.com/questions/876239/how-to-redirect-and-append-both-standard-output-and-standard-error-to-a-file-wit – user3159253 May 15 '22 at 22:56

1 Answers1

1

You can supress the output of a program redirecting it to the special file /dev/null.

Note that there are two kind of output, normal output (stdout) and error output (stderr)

To redirect all output use

command_or_function [args] &>/dev/null

If you want to supress the output for multiple commands, use { }

{
  ...
  commands
  ...
} &>/dev/null

The redirection can be placed at any position, even before command.

&>/dev/null echo hello

To redirect only stdout use >

To redirect only stderr use 2>

See more here

nadapez
  • 2,603
  • 2
  • 20
  • 26