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

UnixDateEcho

Unix Problem Overview


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?

Unix Solutions


Solution 1 - Unix

This will do it:

 echo "Hi, today is $(date)"

Solution 2 - Unix

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

Solution 3 - Unix

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.

Solution 4 - Unix

Backtick will do the trick:

echo "Hi, today is" `date`

Solution 5 - Unix

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDoug SmithView Question on Stackoverflow
Solution 1 - UnixjlliagreView Answer on Stackoverflow
Solution 2 - UnixzellioView Answer on Stackoverflow
Solution 3 - UnixJosepanaeroView Answer on Stackoverflow
Solution 4 - UnixultraView Answer on Stackoverflow
Solution 5 - UnixWilliam PursellView Answer on Stackoverflow