Adding time stamp to log file in bash script Adding time stamp to log file in bash script bash bash

Adding time stamp to log file in bash script


Note you are outputting to Debug.log, while you should indicate the full path of that file: echo "Time: $(date)" >> /path/to/Debug.log.


In general, whenever you want to add timestamp to a log file, you can use:

echo "Time: $(date). Some error info." >> /path/to/your/file.log

date will expand to something like Fri Sep 9 12:18:02 CEST 2016. In that case, you may prefer to use some date flags to have a more parseable date:

$ date "+%FT%T"2016-09-09T12:18:23

Or, even better, using the ISO 8601 format:

$ date -Iseconds2016-09-09T12:18:23+0200

All together:

echo "Time: $(date -Iseconds). Some error info." >> /path/to/your/file.log


Possible reason is different paths for date in terminal and sh: try using full path to the date which you use directly from command line, i.e. $(/bin/date)