HowTo: Get Current Time In Shell Script

How do I get the current server time in shell script on Linux or Unix-like operating systems? How do I store the current time in the shell variable and use in my scripts?

You can use the date command to display or set the current date and time. You need to use the date FORMAT syntax to controls the output of the date command. The %T format sequence interpreted by the date command to
Tutorial details
Difficulty level Easy
Root privileges No
Requirements None
Est. reading time Less than a minute
display the current time. The syntax is:

date +%FORMAT
date +"%FORMAT"
var=$(date +"%FORMAT")

Example: Show current time

Open a terminal and type the following command:

date +"%T"

Sample outputs:


To store time to a shell variable called now, enter:

now=$(date +"%T")
echo "Current time : $now"

Sample outputs:

Current time : 13:31:55

Example: 12 hour clock time

Pass the %r format to the date command:

date +"%r"

Sample outputs:

01:37:06 PM

To remove AM or PM from the output use, type:

date +"%I:%M:%S"

Sample outputs:


Sample shell script

# Purpose: Demo date command and menus 
# Author: nixCraft <> under GPL v2.x+
# ------------------------------------------------------
# Display text at given row and column 
        local x=$1
        local y=$2
        local txt="$3"
        # Set cursor position on screen
        tput cup $x $y
        echo "$txt"
while [ : ]
        # Get the system time
        now="$(date +"%r")"
        # Show main - menu, server name and time
        show 10 10 "MAIN MENU for $HOSTNAME - $now"
        show 11 10 "1. System info"
        show 12 10 "2. Apache server stats"
        show 13 10 "3. MySQL server stats"
        show 14 10 "4. Firewall stats"
        show 15 10 "5. Exit"
        tput cup 16 10; read -t 2 -p "Choice [1-5] ? " usrch
        # do something
        case $usrch in
                1) read -t 2 -p "Showing system info, wait..." fakeinput;;
                2) read -t 2 -p "Showing apache info, wait..." fakeinput;;
                3) read -t 2 -p "Showing mysqld info, wait..." fakeinput;;
                4) read -t 2 -p "Showing firewall info, wait..." fakeinput;;
                5) echo "Bye."; exit 0;;

Run it as follows:

chmod +x

Sample outputs:

Animated gif 01: script in action. Please note time is updating as screen refresh due to the clear command.

  • See date command man page for more information.

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

🐧 7 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
7 comments… add one
  • richardFat Jun 16, 2013 @ 5:07

    ow=$(date +”%T”)
    should be
    now=$(date +”%T”)

  • santhosh Sep 26, 2013 @ 15:51

    i need a shell script to implement backround process that will print current time (**not in command prompt)

  • dilip Jan 30, 2014 @ 7:53

    Hi great Job.

    if i want to run my shell script for only five minutes but that should be in can i do this??

    Could you please give some examples ASAP?

    • Gowtham Feb 27, 2014 @ 5:42


      put your shell script under crontab configure it as per your requirement

  • Gajanan Sep 21, 2014 @ 7:27

    I need shell script which need to check one folder every day and list the files and send a mail to mail box..

    ex: I have below folder
    #cd /image/abc/20140921/
    #/image/abc/20140921/ ls -ltr *gif* | wc

    but here last date folder name will change as each day script has to check every days files and send mail..

    so please help me here..


  • lionel Jan 19, 2015 @ 18:24

    just the date command will do .

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