How do I append the UNIX command date to an echo statement
Asked Answered
B

6

60

Basically I want to have the terminal output a message followed by the date and time, like "Hi, today is -dateandtime-".

So echo can accomplish the first bit, and date can accomplish the last, but only separately, how can I put them together (in one command) so they output together.

Like

echo hello there

-new command-

date

Does it, but not in one line. Is pipelining the answer?

Bid answered 2/2, 2012 at 0:16 Comment(0)
E
100

This will do it:

 echo "Hi, today is $(date)"
Efficiency answered 2/2, 2012 at 0:18 Comment(0)
N
19

Date time will take in an arbitrary format string.

> date +"Hi, today is - %a %b %e %H:%M:%S %Z %Y"
  Hi, today is - Thu Feb 2 03:28: CET 2012
Nodal answered 2/2, 2012 at 0:19 Comment(0)
S
6
echo Hello there, today is `date`

You can also format the output of date using modifiers like:

echo Hello there, today is `date +%D`

See man date for a complete list of the modifiers.

Shuck answered 2/2, 2012 at 0:19 Comment(0)
B
4

Backtick will do the trick:

echo "Hi, today is" `date`
Burlington answered 2/2, 2012 at 0:21 Comment(0)
O
2

For this particular problem, mimisbrunnr's solution is the right way to go. For the general question of how to append data to an echo, some common techniques are:

$ echo 'Hi, today is ' | tr -d '\012'; date
Hi, today is Wed Feb  1 18:11:40 MST 2012
$ echo -n 'Hi, today is '; date
Hi, today is Wed Feb  1 18:11:43 MST 2012
$ printf 'Hi, today is '; date
Hi, today is Wed Feb  1 18:11:48 MST 2012
Oraorabel answered 2/2, 2012 at 1:2 Comment(0)
W
0

For me

$ date +"Hi, today is - %Y%m%d%H%M%S"
Hi, today is - 20240209004703
Wolsky answered 8/2 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.