There are several ways to save the output of a shell script. The easiest way doing so is by simply appending the output to a textfile via ‘>’:
script.sh > logfile.log
The problem with this solution is that not every output is saved to the log file. If a program like ‘curl’ is called, it’s outputs are not appended to the logfile.
This has to do with the types of information a program is sending to the screen/terminal. One type regarding standard output information is called ‘STDOUT’ the other regarding standard error information is called ‘STDERR’. There are three ways in getting this information and writing it into a log file. The script function to do so is called ‘exec’ and has three output options
Standard Output Information: The logfile output of ‘exec 1’ looks basically the same as appending a logfile to a script.
#!/bin/bash exec 1>> logfile.log # script # ...
Standard Error Information: By calling ‘exec 2’ just error messages are written to the log file.
#!/bin/bash exec 2>> logfile.log # script # ...
Combined Output and Error Information: Writing both output and error information to the logfile, call ‘exec &’.
#!/bin/bash exec &>> logfile.log # script # ...