How to: Add or display today’s date from a shell script
Q. How can I display or show today's UNIX computer date from a shell script using echo command? How do I store date command output to a variable?
A. Date command is use to print or set the system date and time under Linux/Unix like operating systems. However some time you need to include today's date in shell script. You need to use command substitution in your shell script to display today's date. Bash/sh shell performs the expansion by executing command and replacing the command substitution with the standard output
of the command, with any trailing newlines deleted. Command substitution allows the output of a command to replace the command name. You can use following syntax:
$(command)
OR
`command`
For example, type the following at shell prompt to display today's date:
$ echo "Today is $(date)"
Output:
Today is Sat Jan 28 15:48:11 IST 2006
Here is sample script, that stores today's date in TODAY and hostname in HOST variable:
#!/bin/bash TODAY=$(date) HOST=$(hostname) echo "-----------------------------------------------------" echo "Date: $TODAY Host:$HOST" echo "-----------------------------------------------------" # add rest code...
Save above script and execute it. See working shell script that notify admin user if Linux/FreeBSD system load crossed certain limit
Capture each and every moment of life with a FREE 4 GB SD memory card with the purchase of select digital cameras from Canon, Nikon, Pentax and others from Amazon
E-mail
Print
Can't find an answer to your question? Contact us
Related Other Helpful FAQs:
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: admin user, command substitution, computer date, echo command, echo echo, freebsd system, host host, host hostname, jan 28, linux freebsd, operating systems, sat jan, shell script, syntax, unix computer



October 21st, 2008 at 3:45 pm
Thanks a lot!! XD
November 6th, 2008 (4 weeks ago) at 1:25 pm
I have a question in regards to creating a bash shell script using the date command to calculate someones’ age. The user provides the script with the month and year in which they were born, but i am having trouble figuring out how to use the date command with the user input to calculate age. Any info in regards to this matter would be greatly appreciated. Thanks