赞
踩
我尝试在我的bash shell脚本中使用$(date)
,但我希望日期为YYYY-MM-DD格式。 我怎么得到这个?
你可以这样做:
$ date +'%Y-%m-%d'
在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 shell
- printf -v date '%(%Y-%m-%d)T\n' -1
-
- # put current date as yyyy-mm-dd HH:MM:SS in $date
- printf -v date '%(%Y-%m-%d %H:%M:%S)T\n' -1
-
- # to print directly remove -v flag, as such:
- printf '
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。