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

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


You can try:

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

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


You can also use the shorter format

From the man page:

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

Example:

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


simple:

today="$(date '+%Y-%m-%d')"yesterday="$(date -d yesterday '+%Y-%m-%d')"