0

I am writing a bash script and I need the date of the Monday of the current week.

date -d monday

returns the correct date if it is Monday. And

date -d last-monday

returns the correct date at Tuesday until Sunday.

I can use an if-clause in the script but I am interested if there is an oneliner too?

steff123
  • 441
  • 1
  • 4
  • 10

2 Answers2

0

Assuming a week starts on Sunday, how about:

date -d "$(( 1 - $(date +%w) )) days"
tshiono
  • 21,248
  • 2
  • 14
  • 22
0
date -d "next-monday - 7 days"

date -d "$(($(date +%u) -1)) days ago"


# today
date  +%Y%m%d
20220524

# Monday
$ date -d "next-monday - 7 days" +%Y%m%d
20220523
# or
$ date -d "$(($(date +%u) -1)) days ago" +%Y%m%d
20220523
ufopilot
  • 3,269
  • 2
  • 10
  • 12