Append Outputs And Errors To File

To write both the output and the errors of a program to a file, we can use the &>> on newer versions of bash to append (not write over) either standard outputs or standard errors to a file.

Create Code That Will Produce An Output

pwd
/home/chris/example_directory

Create Code That Will Produce An Error

pwd -thisisafakeargumenttoproduceerror
-bash: pwd: -t: invalid option
pwd: usage: pwd [-LP]

Create File To Hold Output And Errors

touch log.txt

Append Working Code’s Output To File

pwd &>> log.txt

Append Error-Producing Code’s Error To File

pwd -thisisafakeargumenttoproduceerror &>> log.txt

View File Contents To Show Both Output And Error

cat log.txt
/home/chris/example_directory
-bash: pwd: -t: invalid option
pwd: usage: pwd [-LP]