Get current time (and date) WITHOUT opening a subshell

498 Views Asked by At

Is it possible to get current time ( and possibly date ) without doing it via a subshell?

because if I'm not mistaken, this command do open a subshell?

d=$(date)
1

There are 1 best solutions below

0
On BEST ANSWER

With Bash≥4.2 you can use printf with the %(datefmt)T format:

printf '%(%c)T\n' -1

-1 means now.

See The Bash reference at the printf entry.

To put it in a variable (and hence not use a subshell):

printf -v d '%(%c)T' -1
echo "$d"