I'm launching a course about Next.js + React.jsLearn more

Prefix stderr redirect with timestamp

2018-05-06 · 1 min read
foo() { while IFS='' read -r line; do echo "$(date) $line" >> file.txt; done; };

It creates a function which reads one line of raw (-r) input from stdin. IFS='' prevents ignoring blank lines. It, then, prepends each line with a timestamp.

Test it

echo 42 > >(foo)

or with tee

echo 42 | tee >(foo)

My Tech Newsletter

Get emails from me about programming & web development. I usually send it once a month