Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

date

unix

echo

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?

like image 729
Doug Smith Avatar asked Feb 02 '12 00:02

Doug Smith


People also ask

How do I use the date command in echo?

Sample shell script to display the current date and time #!/bin/bash now="$(date)" printf "Current date and time %s\n" "$now" now="$(date +'%d/%m/%Y')" printf "Current date in dd/mm/yyyy format %s\n" "$now" echo "Starting backup at $now, please wait..." # command to backup scripts goes here # ...

How do you add a date to a command in Linux?

This is pretty easy. Just use the date command with the + option. We can use backticks to capture the value in a variable. You can change the date format by using different % options as detailed on the date man page.


2 Answers

This will do it:

 echo "Hi, today is $(date)" 
like image 180
jlliagre Avatar answered Sep 24 '22 16:09

jlliagre


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 
like image 25
zellio Avatar answered Sep 25 '22 16:09

zellio