0
0
Bash-scriptingConversionBeginner · 2 min read

Bash Script to Convert Unix Timestamp to Date

Use the Bash command date -d @ to convert a Unix timestamp to a readable date, for example, date -d @1633072800 outputs the date for that timestamp.
📋

Examples

Input0
OutputThu Jan 1 00:00:00 UTC 1970
Input1633072800
OutputFri Oct 1 00:00:00 UTC 2021
Input9999999999
OutputSat Nov 20 17:46:39 UTC 2286
🧠

How to Think About It

To convert a Unix timestamp to a date, think of the timestamp as the number of seconds since January 1, 1970. The Bash date command can interpret this number and display it as a normal date by using the -d option with an @ symbol before the timestamp.
📐

Algorithm

1
Get the Unix timestamp input from the user or script argument.
2
Use the <code>date</code> command with the <code>-d</code> option and prefix the timestamp with @.
3
Print the output which is the human-readable date.
💻

Code

bash
#!/bin/bash

# Read timestamp from argument
timestamp=$1

# Convert and print date
converted_date=$(date -d @$timestamp)
echo "$converted_date"
Output
Fri Oct 1 00:00:00 UTC 2021
🔍

Dry Run

Let's trace the timestamp 1633072800 through the code

1

Get input

timestamp=1633072800

2

Convert timestamp to date

date -d @1633072800 returns 'Fri Oct 1 00:00:00 UTC 2021'

3

Print result

Output: Fri Oct 1 00:00:00 UTC 2021

StepActionValue
1Input timestamp1633072800
2date command outputFri Oct 1 00:00:00 UTC 2021
3Printed outputFri Oct 1 00:00:00 UTC 2021
💡

Why This Works

Step 1: Understanding Unix timestamp

A Unix timestamp counts seconds from 1970-01-01 00:00:00 UTC, so converting it means finding the date that many seconds later.

Step 2: Using date command

The Bash date command with -d @timestamp tells the system to interpret the number as seconds since epoch and format it as a date.

Step 3: Output formatting

The output is a human-readable date string showing day, month, time, and timezone for easy understanding.

🔄

Alternative Approaches

Using printf and date
bash
#!/bin/bash

timestamp=$1
printf '%(%Y-%m-%d %H:%M:%S)T\n' "$timestamp"
This uses Bash's built-in printf with format specifiers for a cleaner date format but requires Bash 4.2+.
Using Python one-liner
bash
python3 -c "import datetime, sys; print(datetime.datetime.utcfromtimestamp(int(sys.argv[1])))" 1633072800
Runs a Python command to convert timestamp, useful if date command is limited or unavailable.

Complexity: O(1) time, O(1) space

Time Complexity

The conversion uses a direct system call without loops, so it runs in constant time.

Space Complexity

Only a few variables are used to hold input and output, so space usage is constant.

Which Approach is Fastest?

Using the Bash date command is fastest and simplest; Python adds overhead but offers flexibility.

ApproachTimeSpaceBest For
Bash date -d @timestampO(1)O(1)Simple and fast conversion
Bash printf with formatO(1)O(1)Formatted output with Bash 4.2+
Python one-linerO(1)O(1)When date command is limited or for complex formatting
💡
Always prefix the timestamp with @ when using the date command to convert Unix timestamps.
⚠️
Forgetting the @ prefix before the timestamp causes date to treat it as a normal string, not a timestamp.