How to format dates in shell scripts

in date time, shell scripts

When using dates in a shell script, you can format them like this:

$(date +[format])

Here are some examples:

$(date +”%Y%m%d)



$(date +”%Y-%m-%d)


$(date +%D) which is shorthand equivalent for $(date +”%m/%d/%Y)


To include time:

$(date +%r)

10:15:36 PM


$(date +%T)


A typical date format for a log entry:

$(date +”%Y-%m-%d %T”)

2008-05-29 14:31:34

To test a date format on the command line:

echo $(date +”%Y-%m-%d %T”)

2008-05-29 14:31:34

To see all the date format options, look at the man page for date:

man date

Comments on this entry are closed.

Previous post:

Next post: