How to Create a Timestamp Variable in Bash Script?

A timestamp is a set of characters or encoded data that identifies the occurrence of a specific event, typically denoting the date and time. The timestamp format varies depending on the system or application generating it, but in Bash Script Linux, the most common format is YYYY-MM-DD HH:MM:SS.

They can be incredibly useful when debugging scripts or analyzing log files. This helps you track the progress of your scripts and pinpoint exactly when a particular event occurred, making it easier to troubleshoot any issues that may arise.

This article will discuss how a user can create a timestamp using the following examples.

  • How to Create a Timestamp Variable in Bash?
  • Using the date Command
  • Using the printf Command
  • Logging Script Start and End Times
  • Creating a Backup

How to Create a Timestamp Variable in Bash?

Depending on your requirements and preferences, there are several ways to create a timestamp variable in Bash. Here are some common methods:

Example 1: Using the date Command

The date command is a standard Unix command that prints the current date and time in various formats. To store the output of the date command in a variable, you can use command substitution, which is done by enclosing the command in $() or backticks (`):

#!/bin/bash
timestamp=$(date +'%Y-%m-%d %H:%M:%S')
echo "The current timestamp is $timestamp"

Code Explanation:

– The date command is used with the +%Y-%m-%d %H:%M:%S format string.

– The output is stored in the timestamp variable, which can be used later in the script.

The output of the code can be seen by executing the bash script below:

$ bash time_stamp.sh

Example 2: Using the printf Command

The printf command is versatile and can format and print various data types, including dates and times. To create a timestamp string with printf, you can use the date command to get the current time in seconds since the Unix epoch (January 1, 1970) and then format it as desired. Here’s an example:

#!/bin/bash
epoch=$(date +%s)
timestamp=$(printf "%(%Y-%m-%d %H:%M:%S)T" "$epoch")
echo "The current timestamp is $timestamp"

Code Explanation:

– The date +%s command returns the current time in seconds stored in the epoch variable. 

– The printf command then uses the %()T format specifier to convert the epoch time to a timestamp string in the specified format. 

– The resulting string is stored in the timestamp variable.

The output of the code can be seen by executing the bash script below:

$ bash time_stamp.sh

Example 3: Logging Script Start and End Times

To log the start and end times of a Bash script, you can create two timestamp variables at the beginning and end of the script and then use them in your log messages. Here’s an example:

#!/bin/bash
start_time=$(date +'%Y-%m-%d %H:%M:%S')
echo "Script started at $start_time"
# Your script commands here
echo "Hello, world!"
end_time=$(date +'%Y-%m-%d %H:%M:%S')
echo "Script ended at $end_time"

The output of the code can be seen by executing the bash script below:

$ bash time_stamp.sh

Example 4: Creating a Backup

A user can generate the backup of any directory by incorporating the time stamp, and one of its examples in the form of a Bash script is mentioned below:

#!/bin/bash
timestamp=$(date +'%Y-%m-%d_%H-%M-%S')
mkdir "backup_$timestamp"
cp -r /home/foss/Documents "backup_$timestamp/"

Code Explanation:

– The cp command copies the contents of the /home/foss/documents directory to the backup directory named backup_<timestamp>.

– The <timestamp> is a string representing the current date and time in the format YYYY-MM-DD_HH-MM-SS.

The execution of this bash script will create a new folder with the name of a “backup_$timestamp”.

$ bash time_stamp.sh

Conclusion

The use of timestamps in Linux bash script comes with many benefits, such as a user can track log files for auditing and troubleshooting purposes. Another benefit is that users can manage files or schedule tasks and automate processes. This article has discussed in detail how a user can create a bash script with a timestamp by discussing multiple examples.