0

lets say I want to pipe the date output in the beggining of some text

for example

echo "this line is test line" | date

and expected output should be

Wed May 22 14:55:10 UTC 2024 this line is test line

how to insert the date and time before the line?

2 Answers 2

0

The command you can use is

echo $(date) "this line is test line" 

This will echo the result of execution of command date and append to the end the desired text

1

You might be interested in the ts command:

$ echo "hello world" | ts
May 23 08:41:46 hello world

$ echo "hello world" | ts '%F %T -'
2024-05-23 08:42:46 - hello world

Or, bash's builtin printf can do strftime:

$ printf '%(%c)T - %s\n' -1 "hello world"
Thu May 23 08:46:39 2024 - hello world

$ printf '%(%F %T)T - %s\n' -1 "hello world"
2024-05-23 08:47:26 - hello world
  • The %()T printf directive takes the time format inside the parentheses.
  • -1 is a value that means "now"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .