幕布斯6054654
在bash(> = 4.2)中,最好使用printf的内置日期格式器(bash的一部分),而不是外部的date(通常是GNU日期)。因此:# put current date as yyyy-mm-dd in $date# -1 -> explicit current date, bash >=4.3 defaults to current time if not provided# -2 -> start time for shellprintf -v date '%(%Y-%m-%d)T\n' -1 # put current date as yyyy-mm-dd HH:MM:SS in $dateprintf -v date '%(%Y-%m-%d %H:%M:%S)T\n' -1 # to print directly remove -v flag, as such:printf '%(%Y-%m-%d)T\n' -1# -> current date printed to terminal以bash(<4.2):# put current date as yyyy-mm-dd in $datedate=$(date '+%Y-%m-%d')# put current date as yyyy-mm-dd HH:MM:SS in $datedate=$(date '+%Y-%m-%d %H:%M:%S')# print current date directlyecho $(date '+%Y-%m-%d')可以从日期手册页中查看其他可用的日期格式(对于外部非bash专用命令):man date