16

I was going through a tutorial and there I came across a command:

printf "%(%s)T"

The output of this command is:

1454299615

Could anybody please tell me what is this command and what the output means?

1
  • 1
    This seems to be shell dependent. Is your printf a builtin? /usr/bin/printf on Ubuntu 14.04 gives an error: printf: %(: invalid conversion specification. If you're using bash. Try man bash for builtin documentation.
    – arielf
    Feb 6, 2016 at 20:27

1 Answer 1

19

It's a way to express the formatted time by printf.

The format is:

%(FORMAT)T

Where FORMAT is defined by strftime(3).

So to get the epoch time (Time in seconds since 1970-01-01 00:00:00 UTC), we need strftime(3) format %s:

printf "%(%s)T\n"

Note that you also need \n at the end to add a newline as printf (unlike echo) does not add it by default.

Example:

$ printf "%(%s)T\n"
1454300377

$ printf "%(%Y-%m-%d)T\n"
2016-02-01

$ printf "%(%Y-%m-%d %H:%M:%S)T\n"
2016-02-01 10:20:27

Just for the sake of completeness, you can also use date command in a similar strftime(3) formatted manner to get the time:

$ date '+%s'
1454300542

$ date '+%Y-%m-%d'    ## Short form: date -I
2016-02-01

$ date '+%Y-%m-%d %H:%M:%S'
2016-02-01 10:22:47

NOTE: This behavior is specific to bash's and ksh's built-in function printf, and doesn't work with /usr/bin/printf , csh, and zsh built-ins.

3
  • @rtecxs I have moved your edit in a different place just to keep things clear..thanks anyway :)
    – heemayl
    Feb 1, 2016 at 4:33
  • Is this feature documented? I didn't see it in the printf(1) man page. Feb 1, 2016 at 15:36
  • @NateEldredge printf is a builtin here (although the external one has the same behavior)..check help printf ..
    – heemayl
    Feb 1, 2016 at 16:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .