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?
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 # ...
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.
This will do it:
echo "Hi, today is $(date)"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With