How to Print Time in Bash? [2 Quick Methods]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

To print the current time in Bash, use the command:

echo $(date +%T)

Printing current time is useful for logging purposes, timing operations as well as displaying real-time information. Using the date command with proper format specifiers one can display the time in various formats such as hours, minutes, and seconds, either in 24-hour or 12-hour clock notation. In this article, I am going to demonstrate how to print time in Bash and customize it.

Format Specifiers for Printing Time in Bash

Some useful format specifiers for printing time are listed below:

Format Description Example code Sample Output
%H Hour in 24-hour format $ date +%H 23
%I Hour in 12-hour format $ date +%I 12
%M Print minutes of current hour $ date +%M 46
%s Print seconds of the current minute $ date +%s 35
%z +hhmm numeric time zone $ date +%z +0600
%:z +hh:mm numeric time zone $ date +%:z +06:00
%::z +hh:mm:ss numeric time zone $ date +%::z +06:00:00
%:::z numeric time zone with : to necessary precision(+06, -05:30) $ date +%:::z +06
%p AM/PM designation

1. Printing Time Using the “date” Command

The date command by default prints the current date and time. Also, it provides many options and format specifiers that allow users to print the current time without printing the date. For example:

  1. Print time with date:
    echo $(date)
  2. Print time:
    echo $(date +%T)
  3. Print hour and minute:
    echo $(date +%H:%M)
  4. Print time with AM/PM notation:
    echo $(date +%r)

The following script prints time in all available formats:

#!/bin/bash

today=$(date)
echo "Today's date: $today"

time_with_seconds=$(date +%T)
echo "Current time (with seconds): $time_with_seconds"

time_without_seconds=$(date +%H:%M)
echo "Current time (without seconds): $time_without_seconds"

with_Am_Pm=$(date +%r)
echo "Current time (12-hour format): $with_Am_Pm"

current_hours=$(date +%H)
echo "Current hours: $current_hours"

Executing this script will output as:
How to print time in Bash in different format

2. Printing Time Using the “timedatectl” Command

The timedatectl command is useful to configure the system’s date and time settings. But one can creatively use this command to get the system’s time. Follow the script below to visualize this:

#!bin/bash

time=timedatectl | awk '/Local time:/ {print $5}'
echo $time
EXPLANATION

The given Bash script captures the local time by executing the timedatectl command and extracting the fifth field from the line containing “Local time:” using the awk command. The extracted time value is assigned to the time variable using command substitution. Finally, the script displays the value stored in time using the echo command.

printing time using timectl commandWhile running the program it shows the current time as the image shows above. By default, this time is in a 24-hour time format.

How to Get the Run Time Using the “time” Command in Bash?

To get the run time of a Bash script, use time command before the commands to execute the script. The syntax is:

time ./example.sh

This will print the time required to execute the example.sh script.
Let’s check the execution time of the following script:

#!/bin/bash

echo "Enter a number:"
read n
val=0
power=1

while [ $n -ne 0 ]
do
r=$(( $n % 2 ))
val=$(( $r * $power + $val ))
power=$(( $power * 10 ))
n=$(( $n / 2 ))
done

echo "Binary output: $val"

This script takes a decimal number from the user and converts that into a binary equivalent, Now, execute this using time command before:DIfferent system timeThis time I run the script by adding the time command as a prefix. You can see that the given input is again 4 and it successfully converts the number into its binary representation. Moreover, it shows three different times namely real, user, and sys time.

Note: The real time also known as the wall clock time represents the actual elapsed time from the start of the script until its completion. The user time which refers to the amount of CPU time consumed in user mode by the executed script. And, the sys time represents the amount of CPU time consumed in the kernel mode by the convert_binary.sh script while execution.

Conclusion

In conclusion, printing the local time or CST time is easier with the date command. On the other hand, one can use the time command to print system time. I believe with the knowledge of the above discussion, you can now print the local time in your machine as well as the system time of a Bash script.

People Also Ask

How to print time in Linux?

To print time in the terminal of Linux or any Unix operating system, you must go to the terminal and type the date command.

How to get currenct time up to milliseconds in Bash?

To get current time up to milliseconds, use echo $(date +%H:%M:%S:%3N) command.

How to print timestamp in Bash?

To print timestamp in Bash use the date +"%s" command.

Related Articles


<< Go Back to Bash Script Examples | Bash Scripting Basics | Bash Scripting Tutorial

Rate this post
Md Zahidul Islam Laku

Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio

Leave a Comment