59

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?

Doug Smith
  • 29,668
  • 57
  • 204
  • 388

5 Answers5

98

This will do it:

 echo "Hi, today is $(date)"
jlliagre
  • 29,783
  • 6
  • 61
  • 72
18

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
Ernesto Iser
  • 2,524
  • 2
  • 13
  • 18
zellio
  • 31,308
  • 1
  • 42
  • 61
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.

Josepanaero
  • 136
  • 5
4

Backtick will do the trick:

echo "Hi, today is" `date`
ultra
  • 131
  • 3
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
jlliagre
  • 29,783
  • 6
  • 61
  • 72
William Pursell
  • 204,365
  • 48
  • 270
  • 300