I want to extract the next working day from the date generated by the script and assigned to variable last_day

Let's say variable last_day has a value last_day=20190426 i want to get next_day=2019-04-29 if value of last_day=2019-04-17 then next_day=2019-04-18. Scenario is if last_day is friday print next_day as monday else print next day date.

Not sure how can i make below command to work with variable.

date +%Y-%m-%d -d "+$(( ( $(date +%w)==5 )?3:1 )) days"

Tried this, but it's not working.

next_day=$(date +%Y-%m-%d -d "+$(( ( $(${last_day} +%w)==5 )?3:1 )) days")

1 Answer 1


Try this,

 date -d "$last_day +$( if [ `date -d $last_day +%w` == 5 ]; then echo 3; elif [ `date -d $last_day +%w` == 6 ]; then echo 2; else echo 1; fi ) days" +"%Y%m%d"

The input may be of any day. so

  • if input fall on 5th(Friday) day of the week will add 3.
  • if input fall on 6th(Saturday) day of the week will add 2.
  • else add 1 for all other days of the week

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .