-1

For 2 days old date I usually do this

echo $(date -v -2d)

What i am looking to do is something like this

n=5
echo $(date -v -nd)
0

Solved it myself

n=5
echo $(date -v -${n}d)
4
  • 5
    Why the echo? date -v "-${n}d" should be enough. – Stéphane Chazelas Sep 25 '19 at 12:24
  • sure point noted, but why quotes? it works without it as well. – ishandutta2007 Sep 25 '19 at 13:11
  • 2
    See Security implications of forgetting to quote a variable in bash/POSIX shells Leaving it unquoted introduces an unnecessary dependency of that code on the $IFS variable in shells like sh/bash – Stéphane Chazelas Sep 25 '19 at 13:53
  • 3
    BTW, don't be put off by downvoters. You did the right thing to post an answer to your own question after you found it yourself. Note that it's perfectly fine to edit your posts to merge in suggestions given in comments by others. What matters is that in the end we have the most helpful Q's & A's as possible. – Stéphane Chazelas Sep 25 '19 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.