Bandžiau naudoti $(data)
savo bash apvalkalo scenarijuje, tačiau noriu, kad data būtų pateikta YYYYY-MM-DD formatu. Kaip tai gauti?
Bash (>=4.2) geriau naudoti printf' integruotą datos formatavimo priemonę (bash dalis), o ne išorinę date
(paprastai GNU date).
Todėl:
# 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 '%(%Y-%m-%d)T\n' -1
# -> current date printed to terminal
Bash (<4.2):
# put current date as yyyy-mm-dd in $date
date=$(date '+%Y-%m-%d')
# put current date as yyyy-mm-dd HH:MM:SS in $date
date=$(date '+%Y-%m-%d %H:%M:%S')
# print current date directly
echo $(date '+%Y-%m-%d')
Kitus galimus datos formatus galite peržiūrėti date man puslapiuose (išorinėms ne "bash" komandoms):
man date