If you are on MacOS and don't want to install GNU date
(which is what provides the nonstandard -d
option which you'll find in most answers to related questions) you will need to perform relative date calculations yourself. Perhaps like this:
base=$(date +"%s")
for((i=0; i<5; ++i)); do
date -r $((i * 24 * 60 * 60 + base)) +%Y-%m-%d
done
This avoids any complex date
manipulations; if you need them, they are somewhat unobvious, and less versatile than the GNU date
extensions - see, for example, How to convert date string to epoch timestamp with the OS X BSD `date` command?
For what it's worth, GNU date
(and generally GNU userspace utilities) are the default on most Linux platforms.
With GNU date
you could do date -d @timestamp +format
where MacOS/BSD uses date -r timestamp +format
.
If you need a portable solution, POSIX is not much help here, but a reasonably de facto portable solution is to use e.g. Perl. For inspiration, perhaps review What is a good way to determine dates in a date range?
perl -le 'use POSIX qw(strftime);
$t = time;
for $_ (1..5) {
print strftime("%Y-%m-%d", localtime($t));
$t += 24 * 60 * 60 }'
Demo (on Linux): https://ideone.com/WkAoib
In case it's not obvious, both these solutions get the current time's epoch (seconds since Jan 1, 1970) and then add increments of 24 hours * 60 minutes * 60 seconds to jump ahead one day at a time.