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?

Advertisement

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

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

18 comments… add one
  • 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

    Try

    #! /bin/sh
    #Script to check if noon has passed
    
    hour=`date +%k`
    
    if [ $hour -gt 12 ]
    then time="Afternoon"
    else time="Morning"
    fi
    
    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 today

  • sravan Aug 18, 2020 @ 20:04
    #!/bin/bash
    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 (Author and Admin) Vivek Gite Aug 19, 2020 @ 5:40

      Your syntax is not correct. Try the following format:

      #!/bin/bash
      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. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.