JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Getting Timestamp in Bash

Author:JIYIK Last Updated:2025/04/21 Views:

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 +%sto display the UNIX timestamp using the command. date +%sThe output of is as follows:

1661417510

The output above 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.

Article URL:

Related Articles

Difference between Bash Nohup and &

Publish Date:2025/04/21 Views:188 Category:OPERATING SYSTEM

This short article introduces the nohup command and the control operator to run Linux processes in the background through Bash. In addition, we will further study the key differences between nohup and . Running Linux processes in the backgr

Bash Nohup 与 & 的区别

Publish Date:2025/04/21 Views:186 Category:OPERATING SYSTEM

这篇简短的文章介绍了通过 Bash 在后台运行 Linux 进程的 nohup 命令和 控制运算符。 此外,我们将进一步研究 nohup 和 之间的主要区别。 在后台运行 Linux 进程 Linux 提供了两种在后台运行

Move Multiple Files in Linux Bash

Publish Date:2025/04/06 Views:97 Category:OPERATING SYSTEM

In this article, we will explain how to move multiple files to the same directory in Linux. We will explain different methods such as typing multiple file names, using wildcard characters ( * ) to represent similar file names and/or identic

How to Compare Strings in Bash

Publish Date:2025/04/06 Views:190 Category:OPERATING SYSTEM

We can compare strings using various comparison operators and check if a string contains a substring using regular expressions. String comparison in Bash String comparison means checking whether the given strings are identical or not. Two o

How to Add Comments in Bash Scripts

Publish Date:2025/04/06 Views:123 Category:OPERATING SYSTEM

Comments are lines that are ignored by the interpreter and are only used to describe what is happening in the code or to provide insight into a particular block or line of code. Comments make it easier for the reader to understand the code.

tr Command in Linux Bash

Publish Date:2025/04/05 Views:54 Category:OPERATING SYSTEM

In Linux, we can use Bash scripts to perform string manipulation such as concatenation, truncation, and finding words in a text. tr This article will explain how to translate or remove characters using command in Linux Bash . tr Using comma

Differences between Bash Profile and Bashrc

Publish Date:2025/04/05 Views:188 Category:OPERATING SYSTEM

This article explains the difference between ~./bash_profile and files in Bash. ~/.bashrc What are startup files in Bash? Startup files are files that are executed after the shell is started. The startup files depend on the type of shell th

Differences between Sh and Bash

Publish Date:2025/04/05 Views:145 Category:OPERATING SYSTEM

This article explains what a shell is, find out which shell you are currently using, check a list of all available shells, and the difference between sh and . bash What is a Shell A shell is a computer program that accepts commands. It also

How to Append Text to a File Using Bash

Publish Date:2025/04/05 Views:83 Category:OPERATING SYSTEM

We can use the redirection ( ) operator and tee the command to append text to a file. We have to make sure we have enough permissions to add text to the file. If we don't have enough permissions, we may get a permission denied error. Use th

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial