≡ Menu

HowTo: Get / Print Current Date in Unix / Linux Shell Script

How do I get the current date in Unix or Linux shell scripting and store it into a shell variable? How do I print the current date using Unix shell script?

You need to use the following syntax to print current date and time on screen:


To store current date and time to a variable, enter:

To print this date either use printf or echo statement:

echo "$now"
echo "Current date: $now"

OR use printf:

printf "%s\n" "$n"

You can format and display date using the following syntax:

date +'FORMAT'

### mm/dd/yyyy ###
date +'%m/%d/%Y'

## Time in 12 hr format ###
date +'%r'

Sample shell script

printf "Current date and time %s\n" "$now"
now="$(date +'%d/%m/%Y')"
printf "Current date in dd/mm/yyyy format %s\n" "$now"
echo "Starting backup at $now, please wait..."
# command to backup scripts goes here
# ...
Share this tutorial on:

Like this? Follow us on Twitter OR support us by using Patreon

{ 5 comments… add one }
  • nina November 21, 2014, 3:37 pm

    how to print day of the week?

  • Richard Black June 30, 2015, 5:25 pm

    $ date | awk -F ‘,’ ‘{print $1}’

    • Richard Black June 30, 2015, 5:28 pm

      $ date “+%a”

      • Claudiu September 25, 2015, 2:14 pm

        Just for the sake of diversity :)

        date | grep -o [MTWFS][uoehra][neduit]
        date | cut -d " " -f 1
  • Deovrat Jalgaonkar February 19, 2016, 6:31 pm

    Thank you very much!!

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , , ,