“日期”命令中的“+”有什么作用?

“日期”命令中的“+”有什么作用?

以下给了我一个日期:

$ date +%Y%m%d                                                                             
20201012 

这不会:

$ date %Y%m%d
date: invalid date ‘%Y%m%d’

手册页说+前面有FORMAT.+除了正确的语法之外还有什么重要性?

date [OPTION]... [+FORMAT]

答案1

info date

 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 +"The year is %Y"
The year is 2020

答案2

引入+了日期格式字符串。

相关内容