# Adding timestamp to program outputs

**URL:** <https://linux-tips.com/t/adding-timestamp-to-program-outputs/334>\
**Category:** Shell\
**Created:** [March 27, 2016, 3:46pm UTC](https://linux-tips.com/t/adding-timestamp-to-program-outputs/334 "2016-03-27T15:46:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![redsector](https://linux-tips.com/user_avatar/linux-tips.com/redsector/32/71_2.png) [@redsector](https://linux-tips.com/u/redsector)\
**Post date:** [March 27, 2016, 3:46pm UTC](https://linux-tips.com/t/adding-timestamp-to-program-outputs/334/1 "2016-03-27T15:46:19Z")

</div>

Sometimes it is needed to read a program outputs with generated timestamp which you don’t have source code of that program or doesn’t want to make change on code.

How can I add timestamp information on the output text of the programs with a shell function?

---

<div class="post-metadata">

**Author:** ![demirten](https://linux-tips.com/user_avatar/linux-tips.com/demirten/32/64_2.png) [@demirten](https://linux-tips.com/u/demirten)\
**Post date:** [March 27, 2016, 3:54pm UTC](https://linux-tips.com/t/adding-timestamp-to-program-outputs/334/2 "2016-03-27T15:54:21Z")

</div>

You must send program outputs to a pipe, read lines and add timestamp before the text.

Here is an example function:

```auto
adddate() {
    while IFS= read -r line; do
	printf "\033[1;31m%s\033[0m %s\n" "$(date +%s.%N)" "$line"
    done
}

```

You can copy this function into `~/.bashrc` or any other file read on login.

Example usage:

```auto
$ grep root /etc/passwd | adddate
1459093897.771286856 root:x:0:0:root:/root:/bin/bash

```

Prefix part consist of **unixtimestamp.nanoseconds**.

If you don’t want to make colorful prefix, replace `printf` line as below:

```auto
printf "%s %s\n" "$(date +%s.%N)" "$line"

```
