You can use the following syntax to subtract a specific number of days from a date in Bash:
new_date="$(date -d "$my_date - 10 days" +%Y-%m-%d)"
This particular example will subtract 10 days from the date stored in the variable named my_date and save the results to a new variable named new_date.
To add a certain number of days, simply change the subtraction sign to an addition sign:
new_date="$(date -d "$my_date + 10 days" +%Y-%m-%d)"
Note that the + operator specifies the format to use for the resulting date with the following options:
- +%Y: Year number with four digits
- +%m: Month number with two digits
- +%d: Day of the month with two digits
The following example shows how to use this syntax in practice.
Example: How to Add and Subtract Days from a Date in Bash
This article is currently being written on August 20, 2024.
We can use the following syntax to store this current date in a variable named my_date:
my_date=$(date +"%m-%d-%Y") echo $my_date
The following screenshot shows how to use this syntax in practice:

We can then use the following syntax to subtract 10 days from this date and save it in a new variable named new_date:
new_date="$(date -d "$my_date - 10 days" +%Y-%m-%d)"
The following screenshot shows how to use this syntax in practice:

Notice that when we use the echo statement to view the new_date variable we can see that it contains a date of 2024-08-10, which represents the date from the my_date variable with 10 days subtracted from it.
We could also use the following syntax to instead add 10 days to the my_date variable:
new_date="$(date -d "$my_date + 10 days" +%Y-%m-%d)"
The following screenshot shows how to use this syntax in practice:

Now when we use the echo statement to view the new_date variable we can see that it contains a date of 2024-08-30, which represents the date from the my_date variable with 10 days added to it.
Feel free to change 10 days to any number of specific days that you would like to add or subtract.
Related Tutorials
The following tutorials explain how to perform other common tasks in Bash:
Bash: How to Get Day of Week
Bash: How to Get Last Day of Month
Bash: How to Format a DateTime
Bash: How to Extract Date from String
