How To Get / Print Current Date in Unix / Linux Shell Script

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? How can I display the current time in Linux shell script? \

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

date
date +"%FORMAT"
var=$(date)
var=`date`
echo "$var"

To store current date and time to a variable, enter: now=$(date) OR now=`date`

To print this date either use the printf or echo statement: echo "$now" echo "Current date: $now" OR use the printf command: printf "%s\n" "$now" OR printf "Current date and time in Linux %s\n" "$now"

Getting the current date and time in Linux shell script

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'
 
## backup dir format ##
backup_dir=$(date +'%m/%d/%Y')
echo "Backup dir for today: /nas04/backups/${backup_dir}"

Finding the current date and time in Linux or Unix using the date command

A list of date command format codes

Sample shell script to display the current date and time

#!/bin/bash
now="$(date)"
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
# ...

\

Last updated