Regarding: https://github.com/xero/dotfiles/blob/3b05bdfc36346f8d92a5af0a317c7f769bf7754a/bin/bin/epoch
In BASH 5.0, $EPOCHSECONDS
was introduced, which stores seconds since Epoch, saving the need for all that stuff. Even in >= BASH 4.2, the printf
builtint itself can already show SSE:
The %(...)T
format specification is just for most of the usual gubbins you'd give date(1) and other utilities and functions from other languages (e.g., strftime()
).
Regarding formatting SSE, that's also easy with printf
in BASH:
The above would format the given SSE as a common timestamp.
You most likely want to store the date and/or time into a variable for later usage, but using command substitution and a subshell for this isn't fun. Believe it or not, printf
has you covered there as well, as of BASH 3.1 (4.1 for array subscripts):
printf -v Timestamp '%(%F_%X)T\n' SSE
The above will grab a suitable timestamp, then shove it into a shell variable.
Essentially, date(1) is almost always redundant in BASH, with some obscure exceptions, such as date -d tomorrow
, although this could easily be done with BASH as well. As I've been saying for years: "if you're using BASH, use BASH!" ;) This will help optimize your scripts in the future.