Get seconds since Epoch in Bash
bash
5.0 natively supports retrieving seconds since Epoch using a variable.
Native bash
method
1echo $EPOCHSECONDS # 1682740449
If seconds are preferred:
1echo $EPOCHREALTIME # 1682740449.252199
If milliseconds are preferred:
1echo $(( ${EPOCHREALTIME/./} / 1000 )) # 1682740449252
Using date
In addition to using the native bash
method, date
may be used.
According to the date
manual:
%s seconds since the Epoch (1970-01-01 00:00 UTC)
1echo $(date +%s) # 1682740326
If milliseconds seconds are preferred:
1echo $(date +%s%3N) # 1682740449252