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:

For example, type the following at shell prompt to display today’s date:
$ echo "Today is $(date)"

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:

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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 18 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
18 comments… add one
  • Esteve Oct 21, 2008 @ 15:45

    Thanks a lot!! XD

  • J Nov 6, 2008 @ 13:25

    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

  • Akshay Feb 25, 2009 @ 9:35

    How to check the pervious date in unix.

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

  • web-design-dude Jul 24, 2009 @ 12:54

    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.


  • bobby Jun 19, 2010 @ 14:57

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

  • Nick Johnson Oct 18, 2010 @ 23:11

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

  • vadivel.c Nov 23, 2010 @ 9:40

    How to check the pervious date in shell script.

  • Pratik Parmar Sep 19, 2011 @ 7:36

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

  • Long T Jan 13, 2012 @ 18:52

    Please help. I plan to create the shell scripts that it gives me the date in this format like 2012-01-13. Due i need to move some files from /abc/defg/*.c to /z/abc/2012-01-13. The folder of 2012-01-13 will be change every day and depends on which day I run the scripts. I want to set up as cronjob. Thank in advance

  • Abhishek Jul 1, 2012 @ 16:56

    @web-design-dude #!/bin/sh is fine it works in my ubuntu…. what u need to do is declare variable and then assign the value :
    this works perfectly fine

  • john Aug 14, 2012 @ 7:26

    Can you help me with creating a script that reads the system time and if its before noon the it prints a message saying its the morning and its after noon it says its afternoon etc..

  • Tris Nov 3, 2012 @ 7:50

    Hey John


    #! /bin/sh
    #Script to check if noon has passed
    hour=`date +%k`
    if [ $hour -gt 12 ]
    then time="Afternoon"
    else time="Morning"
    echo "It is about $hour o'clock"
    echo "It is $time"
  • Rob Jun 2, 2015 @ 8:25

    Thank you for this, really helped me.

  • ganesh Nov 4, 2015 @ 6:46

    good program

  • Satyakanta Aug 3, 2016 @ 13:04

    How can I add time stamp to file name.Please help

  • Manas Sep 17, 2016 @ 6:15

    How to Write a simple script which display todays date and create a file in a separate folder along with the date (e.g. /home/script/example_20130916.txt)

  • sravan Aug 18, 2020 @ 20:04
    year='date +%y'
    month='date +%m'
    day='date +%d'
    hour='date +%H'
    minute='date +&%M'
    second='date +%S'
    echo "current date is":'($year-$month-$day)'

    hello, i wrote this script to print the date but i am not getting the output as date.
    my output is like this ($year-$month-$day) can anybody help ?

    • 🐧 Vivek Gite Aug 19, 2020 @ 5:40

      Your syntax is not correct. Try the following format:

      year=$(date +%y)
      month=$(date +%m)
      day=$(date +%d)
      hour=$(date +%H)
      minute=$(date +%M)
      second=$(date +%S)
      echo "current date is :$year-$month-$day"

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum