Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log echo statement with timestamp in shell script

I am currently using following to log stdout and stderr. I want to log echo statements along with timestamp.

exec 3>&1 1>>${LOG_FILE} 2>&1

How can I achieve it using shell

like image 424
Galet Avatar asked Sep 10 '25 21:09

Galet


1 Answers

Depending on the exact intended behavior you may want the ts utility.

% echo message | ts
Apr 16 10:56:39 message

You could also alias echo.

% sh
$ alias echo='echo $(date)'
$ echo message
Mon Apr 16 10:57:55 UTC 2018 message

Or make it a shell function.

#!/bin/sh

echo() {
    command echo $(date) "$@"
}

echo message
like image 143
user464502 Avatar answered Sep 13 '25 13:09

user464502