Bash Avoiding date using printf

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

In Bash 4.2, a shell built-in time conversion for printf was introduced: the format specification %(datefmt)T makes printf output the date-time string corresponding to the format string datefmt as understood by strftime.

Syntax

  • printf '%(dateFmt)T' # dateFmt can be any format string that strftime recognizes
  • printf '%(dateFmt)T' -1 # -1 represents the current time (default for no argument)
  • printf '%(dateFmt)T' -2 # -2 represents the time the shell was invoked

Remarks

Using printf -v foo '%(...)T' is identical to foo=$(date +'...') and saves a fork for the call to the external program date.



Got any Bash Question?