Friday, December 11, 2015

How to add a timestamp to your log files in your bash scripts in Linux


Today I show how to add a timestamp to your log files in your bash scripts. That is easy. In this solution we have to include the function

adddate() {
    while IFS= read -r line; do
        echo "$(date) $line"
    done
}

in your script file and add pipe the command to the function using the systax:

command | adddate

For example:

#!/bin/bash
adddate() {
    while IFS= read -r line; do
        echo "$(date) $line"
    done
}
echo -e "Doing something"
ls -la | adddate