EpochConverter/langs/Bash / Shell

Unix Timestamp in Bash / Shell

Bash can get Unix timestamps with the date command (GNU coreutils or macOS BSD). These one-liners are useful in scripts, cron jobs, and DevOps automation.

Ad

Code Examples

Current timestamp (seconds) — Linux/macOS

date +%s

Works on Linux (GNU date) and macOS (BSD date). Prints the current Unix timestamp in seconds. The most common shell timestamp command.

Current timestamp (milliseconds) — Linux

date +%s%3N

GNU date only (Linux). %s is seconds, %3N is nanoseconds truncated to 3 digits (milliseconds). Not available on macOS BSD date.

Current timestamp (milliseconds) — macOS

python3 -c 'import time; print(int(time.time() * 1000))'

macOS BSD date doesn't support %N. This Python one-liner works on any system with Python 3 installed.

Convert timestamp to human-readable date — Linux

date -d @1708560000

GNU date: the @ prefix treats the argument as a Unix timestamp. Add -u for UTC output: date -u -d @1708560000.

Convert timestamp to human-readable date — macOS

date -r 1708560000

BSD date (macOS): use -r instead of -d @. Add -u for UTC: date -u -r 1708560000.

Store timestamp in a variable

TS=$(date +%s)
echo "Backup started at $TS"

Command substitution captures the output. Useful for logging, naming backup files, or measuring elapsed time in scripts.

Note

GNU date (Linux) and BSD date (macOS) have incompatible flag syntax — %N for nanoseconds works on Linux but not macOS, and -d @timestamp works on Linux while macOS uses -r timestamp. For cross-platform scripts, use Python or Node.js for timestamp operations instead of relying on date flags.

Ad

Need to convert a specific timestamp? Use the live converter — paste any epoch value and see the human-readable date instantly.

← Open the converter