img

HowTo: Get Current Time In Shell Script

September 8, 2019

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 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"

date +"%T"

Sample outputs:

16:33:22

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

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

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"

date +"%r"

Sample outputs:

01:37:06 PM

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

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

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

Sample outputs:

01:39:30

Sample shell script

#!/bin/bash
# Purpose: Demo date command and menus 
# Author: SXI LLC <sxi.io> under GPL v2.x+
# ------------------------------------------------------
 
# Display text at given row and column 
show(){
        local x=$1
        local y=$2
        local txt="$3"
        # Set cursor position on screen
        tput cup $x $y
        echo "$txt"
}
while [ : ]
do
        clear
        # 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;;
        esac
done

#!/bin/bash
# Purpose: Demo date command and menus
# Author: SXI LLC <sxi.io> under GPL v2.x+
# —————————————————— # Display text at given row and column
show(){
local x=$1
local y=$2
local txt="$3"
# Set cursor position on screen
tput cup $x $y
echo "$txt"
}
while [ : ] do
clear
# 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;;
esac
done

Run it as follows:

chmod +x demo.sh
./demo.sh

chmod +x demo.sh
./demo.sh

Sample outputs:

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

References:
  • See date command man page for more information.

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Article Tags:
Article Categories:
How To

Comments are closed.