Skip to main content
1 of 3
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.

Michael Homer
  • 78.9k
  • 17
  • 221
  • 239