Doug Smith
Doug Smith

Reputation: 29326

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

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?

Upvotes: 61

Views: 111905

Answers (6)

tuwilof
tuwilof

Reputation: 587

For me

$ date +"Hi, today is - %Y%m%d%H%M%S"
Hi, today is - 20240209004703

Upvotes: 0

zellio
zellio

Reputation: 32514

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

Upvotes: 19

William Pursell
William Pursell

Reputation: 212474

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

Upvotes: 2

ultra
ultra

Reputation: 141

Backtick will do the trick:

echo "Hi, today is" `date`

Upvotes: 5

Josepanaero
Josepanaero

Reputation: 136

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.

Upvotes: 6

jlliagre
jlliagre

Reputation: 30833

This will do it:

 echo "Hi, today is $(date)"

Upvotes: 102

Related Questions