How to: Add or display today’s date from a shell script

by Vivek Gite on January 28, 2006 · 8 comments

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

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 8 comments… read them below or add one }

1 Esteve October 21, 2008

Thanks a lot!! XD

Reply

2 J November 6, 2008

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

Reply

3 Akshay February 25, 2009

How to check the pervious date in unix.

for eg today is 25th Feb and i want to print 24th Feb

Reply

4 web-design-dude July 24, 2009

Ive just used this on a debian (Ubuntu 8.x) server and had to use $DATE=’date’ instead of (date).

Is this because of Debian/Ubuntu or due to me using #!/bin/sh rather than #!/bin/bash as bad habit.

Thanks
Lien

Reply

5 bobby June 19, 2010

how can i display the date by using shell programming in the UNIX with out using date command ?

Reply

6 Nick Johnson October 18, 2010

sweetness! just helped me add the date command to my backup script!

Reply

7 vadivel.c November 23, 2010

How to check the pervious date in shell script.

Reply

8 Pratik Parmar September 19, 2011

Thank you very much. Really your website is very helpful to me.

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 4 + 15 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: