Getting Timestamps in Bash
This article discusses the date Bash command used to obtain the system date/time and UNIX timestamp.
Get Timestamp Using date Command in Bash
The Linux terminal uses the date command to print the current date and time. The simplest version of the date command can be executed without any parameters.
The following is sample output from the date command, showing the current date and time on the system:
Thu Aug 25 08:43:54 UTC 2022
We can use different parameters in the date command to set a specific format for printing. For example, the command date +"%m-%d-%y"
outputs the date in the following format:
08-25-22
In the above command, %m is used to represent the month, %d is used to represent the day, and %y is used to represent the year. Similarly, we can use many different formats of date commands.
We can only date +"%T"
print the current time using command. %T is used to print time only.
date +"%T"
The output is as follows:
09:01:00
Get UNIX timestamp
We can use the date command date +%s
to display the UNIX timestamp using the command. date +%s
The output of is as follows:
1661417510
The above output is a UNIX timestamp.
Getting Date/Time in Bash Scripts
We can also use the command in Bash scripts date
. Consider the following Bash script myscript.sh which displays the current date on the terminal:
#!/bin/bash
echo $(date)
When we execute the above script using ./myscript.sh, this will print the current date on the terminal.
Store a UNIX timestamp in a variable
We can also store the current date in any format using the date command in a variable.
Consider the following script:
#!/bin/bash
timestamp=$(date)
echo $(timestamp)
In the following script, timestamp is a variable in which we store the current date. When we execute the above script, it displays the value of timestamp variable on the terminal console due to echo command.
For example, we could use:
- timestamp=$(date +%s) stores the UNIX timestamp in the variable timestamp.
- timestamp=$(date +%T) stores the current time in the variable timestamp.
- timestamp=$(date +"%m-%d-%y") stores the current date in the variable timestamp.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Running Regular Expressions in Case Statements in Bash
Publish Date:2025/03/22 Views:75 Category:OPERATING SYSTEM
-
This article explores regular expressions, their basic syntax, and how to run them in Bash using case and if-else constructs. Introduction to Regular Expressions A regular expression, also called regex or regexp, is a sequence of characters
Sending a message to logged in users in Bash
Publish Date:2025/03/22 Views:168 Category:OPERATING SYSTEM
-
This article explores methods to send data to another logged in user in Bash. This article discusses methods to find active users and send them messages. Finding Online Users in Bash Before sending data to an online user, you must verify wh
Bash copy all files from remote directory
Publish Date:2025/03/22 Views:156 Category:OPERATING SYSTEM
-
This article describes methods for copying files between different hosts on a network. It also explores the scp command and how to use it to copy files from a remote location. scp command Secure Copy Protocol (SCP) is a secure copy network
Floating-point arithmetic in Bash
Publish Date:2025/03/22 Views:189 Category:OPERATING SYSTEM
-
This short article describes how to quickly perform floating-point calculations in GNU BASH (shell scripts) directly at the command prompt or in a shell script. If you work with numbers, it can be helpful to perform quick floating-point cal
Writing to a File in Bash
Publish Date:2025/03/22 Views:133 Category:OPERATING SYSTEM
-
This article will let us explore different ways to write files in bash. Files can be appended or overwritten as required. Let's see how we can do it. Different ways to write/overwrite files in Bash We will see several operators, such as and
Terminating a Process in Bash
Publish Date:2025/03/22 Views:135 Category:OPERATING SYSTEM
-
This article will first discuss the different concepts related to Linux processes. After this, we will learn the different ways to terminate a process. Before going into the kill command, we must understand some preliminary concepts. Simple
Solve R command not found on Bash (or Cygwin)
Publish Date:2025/03/21 Views:102 Category:OPERATING SYSTEM
-
Commands may sometimes behave differently than you expect, even if it appears that you have done everything right, such as in the case of bash: '\r': command not found or similar error messages, for example syntax error near unexpected toke
Error handling in Bash
Publish Date:2025/03/21 Views:94 Category:OPERATING SYSTEM
-
This article introduces error handling in bash. Remember, understanding exit codes, options such as errexit and trap allow us to build robust scripts and manage bash problems more effectively. Exit Codes in Bash Handling errors based on exi
Getting the absolute path in Bash
Publish Date:2025/03/21 Views:60 Category:OPERATING SYSTEM
-
In this Bash article, we will learn different ways to get the absolute path in Linux. We will also learn some different Linux commands to get the absolute path of a file. Before we begin, we need to understand the basic concepts of absolute