Getting the current date in bash without spawning a sub-process

Viewed 213

This question is pure curiosity. It is easy to get a date by running the date command from bash, but it is an external executable and requires spawning a subprocess. I wondered whether it is possible to get the current time/date formatted without a subprocess. I could only find references to date/time formats in the context of PS1 and HISTTIMEFORMAT. The latter allows this:

HISTTIMEFORMAT="%Y-%m-%d_%H:%M:%S "
history -s echo
x=$(history)
set -- $x
date="$2"

This comes close, but the $(history) spawns a subprocess, as far as I can tell.

Can we do better?

2 Answers
Related