How to set a variable to current date and date-1 in linux?

55

I want to set the variable date-today to the current date, and date_dir to yesterday's date, both in the format yyyy-mm-dd.

I am doing this:

#!/bin/bash
d=`date +%y%m%d%H%M%S`
echo $d

This question is tagged with linux bash date

~ Asked on 2013-12-10 03:56:01

The Best Answer is


93

You can try:

#!/bin/bash
d=$(date +%Y-%m-%d)
echo "$d"

EDIT: Changed y to Y for 4 digit date as per QuantumFool's comment.

~ Answered on 2013-12-10 04:16:43


11

You can also use the shorter format

From the man page:

%F     full date; same as %Y-%m-%d

Example:

#!/bin/bash
date_today=$(date +%F)
date_dir=$(date +%F -d yesterday)

~ Answered on 2017-08-08 06:58:31


Most Viewed Questions: