0

I have this test.sh

#!/bin/bash echo $1 

calling it works:

sh ./test.sh $(date +"%F") 

I want to subtract 5 days from current date and pass to test.sh

sh ./test.sh $(date --date="date-5 day" +"%F") 

The line above does not work. What is the correct way?

2
  • You're trying to do something that can't be done without being way more specific, and is kind of @jon-skeet's domain: what does "minus 5 days" mean. Is it 120 hours? In which timezone? Following which calendar? In which locale? Things get rather complicated even if you "just want 5 days ago", because there is no "just" when it comes to date/time calculations. So instead of asking bash for this information, use a date/time CLI utility, or a proper date/time library from a scripting language with a solid enough such library that you can trust the results to be right. Commented Aug 9, 2017 at 3:39
  • 3
    Please see this answer - you want --date="-5 days". Commented Aug 9, 2017 at 3:41

1 Answer 1

1

I suggest:

sh ./test.sh $(date --date="-5 day" +"%F") 

With my settings en_US.UTF-8 command date --date="-5 day" +"%F" outputs 2017-08-04.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.