0

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 Answers1

8

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"
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104