0

"NON-Compliance - PermitRootLogin" printed into output.log & test1.txt. I do NOT want to echo to the terminal. How can I do that?

  echo "NON-Compliance - PermitRootLogin" | tee output.log test1.txt

Terminal: NON-Compliance - PermitRootLogin

jackpaul
  • 1
  • 1
  • 1

1 Answers1

1

In the original title you wrote "silence echo", you also used the tag , but what you see in the terminal does not directly come from echo you invoked. Your command looks like this:

echo … | tee …

In general echo prints to its stdout. echo does not print to anywhere else. echo does not print to the terminal unless the terminal is its stdout. In your command echo prints to a pipe and tee reads from the pipe. This echo does not print to the terminal.

In general tee prints to files passed as operands and to its stdout. In your case its stdout is the terminal, this (not echo) is what you want to silence. You have two options:

  1. Either redirect stdout of tee to one of the files instead of specifying the file as an operand:

    … | tee output.log >test1.txt
    
  2. Or redirect stdout of tee to /dev/null (it's a file that discards everything written to it):

    … | tee output.log test1.txt >/dev/null
    

    (Redirecting stdout and/or stderr to /dev/null is a basic general way to make a command silent.)


There's a subtle difference between the two. In the first case the shell creates (if needed), truncates and opens test1.txt before tee is started. In the second case tee does all this.

There are cases when the difference matters. Imagine you need sudo tee … because test1.txt cannot be accessed by the regular user who runs the shell. In this case >test1.txt wouldn't work. In fact sudo tee some_file >/dev/null is a useful trick to write to some_file that requires root access.

Kamil Maciorowski
  • 69,815
  • 22
  • 136
  • 202