Skip to main content
Break out the parts of explanation to bullet points for easier reading.
Source Link

Use process substitution with & redirection and exec:

exec &> >(tee -a "$log_file")
echo This"This will be logged to the file and to the screenscreen"

$log_file will contain the output of the script and any subprocesses, and the output will also be printed to the screen.

>(...) starts the process ... and returns a file representing its standard input. exec &> ... redirects both standard output and standard error into ... for the remainder of the script (use just exec > ... for stdout only). tee -a appends its standard input to the file, and also prints it to the screen.

  • >(...) starts the process ... and returns a file representing its standard input.

  • exec &> ... redirects both standard output and standard error into ... for the remainder of the script (use just exec > ... for stdout only).

  • tee -a appends its standard input to the file, and also prints it to the screen.

Use process substitution with & redirection and exec:

exec &> >(tee -a "$log_file")
echo This will be logged to the file and to the screen

$log_file will contain the output of the script and any subprocesses, and the output will also be printed to the screen.

>(...) starts the process ... and returns a file representing its standard input. exec &> ... redirects both standard output and standard error into ... for the remainder of the script (use just exec > ... for stdout only). tee -a appends its standard input to the file, and also prints it to the screen.

Use process substitution with & redirection and exec:

exec &> >(tee -a "$log_file")
echo "This will be logged to the file and to the screen"

$log_file will contain the output of the script and any subprocesses, and the output will also be printed to the screen.

  • >(...) starts the process ... and returns a file representing its standard input.

  • exec &> ... redirects both standard output and standard error into ... for the remainder of the script (use just exec > ... for stdout only).

  • tee -a appends its standard input to the file, and also prints it to the screen.

added 40 characters in body
Source Link
Michael Homer
  • 78.9k
  • 17
  • 221
  • 239

Use process substitution with & redirection and exec:

exec &> >(tee -a "$log_file")
echo This will be logged to the file and to the screen

$log_file will contain the output of the script and any subprocesses, and the output will also be printed to the screen.

>(...) starts the process ... and returns a file representing its standard input. exec &> ... redirects both standard inputoutput and standard outputerror into ... for the remainder of the script (use just exec > ... for stdout only). tee -a appends its standard input to the file, and also prints it to the screen.

Use process substitution with & redirection and exec:

exec &> >(tee -a "$log_file")
echo This will be logged to the file and to the screen

$log_file will contain the output of the script and any subprocesses, and the output will also be printed to the screen.

>(...) starts the process ... and returns a file representing its standard input. exec &> ... redirects both standard input and standard output into ... for the remainder of the script. tee -a appends its standard input to the file, and also prints it to the screen.

Use process substitution with & redirection and exec:

exec &> >(tee -a "$log_file")
echo This will be logged to the file and to the screen

$log_file will contain the output of the script and any subprocesses, and the output will also be printed to the screen.

>(...) starts the process ... and returns a file representing its standard input. exec &> ... redirects both standard output and standard error into ... for the remainder of the script (use just exec > ... for stdout only). tee -a appends its standard input to the file, and also prints it to the screen.

Source Link
Michael Homer
  • 78.9k
  • 17
  • 221
  • 239

Use process substitution with & redirection and exec:

exec &> >(tee -a "$log_file")
echo This will be logged to the file and to the screen

$log_file will contain the output of the script and any subprocesses, and the output will also be printed to the screen.

>(...) starts the process ... and returns a file representing its standard input. exec &> ... redirects both standard input and standard output into ... for the remainder of the script. tee -a appends its standard input to the file, and also prints it to the screen.