data +Format 截取日期月份年份,
今天看到一篇 Linux生成随机数的文章。
里面用到 data +%s.
没有见过这样的用法。就开始自己尝试。
$ date
2018年 3月 23日 金曜日 10:17:23
$ date +%d
23
$ date +%m
03
$ date +%y
18
$ date +%s
1521771440
所以这就是用来显示时分秒的。最后这个 +%s 应该是UNIX时间戳。(就是从1970.1.1 00:00:00 到现在的秒数,没验证过。)
在
http://www.gnu.org/software/coreutils/manual/html_node/date-invocation.html#date-invocation
里面看到
If given an argument that starts with a ‘+’, date prints the current date and time (or the date and time specified by the --date option, see below) in the format defined by that argument, which is similar to that of the strftime function. Except for conversion specifiers, which start with ‘%’, characters in the format string are printed unchanged. The conversion specifiers are described below.
这一段前半句是说,通过 '+'可以格式化输出当前时间,后半句说如果 '+' 后面没有 ‘%’会直接输出字符。
测试如下:
$ date +y
y
$ date +%y
18
结束。