sdot257
sdot257

Reputation: 10366

Check to see if today is a certain date

I want to check if the day is Sunday, but for some reason I can't get it to work.

[[ "date '+%a'" == "Sun" ]] && echo "Today is Sunday"

Upvotes: 2

Views: 6164

Answers (5)

Boop
Boop

Reputation: 1386

For completion:

If you have multiple locales I advise to use +%u cf. man date:

%u day of week (1..7); 1 is Monday

Today is monday:

date +%u
1
if [[ $(date +%u) -eq 1 ]]; then
  echo 'ho no :c'
fi
ho no :c

hth

Upvotes: 0

ghostdog74
ghostdog74

Reputation: 342363

case "$(date '+%a')" in "Sun" ) echo "sunday";; esac

Upvotes: 0

Courtland Halbrook
Courtland Halbrook

Reputation: 31

You can use the date +%u to get the day number of the week... 1 - 7 with Monday being 1 that way you should not have an issue with non-english locales

Upvotes: 3

lbedogni
lbedogni

Reputation: 7997

[[ $(date '+%a') == "Sun" ]] && echo "Sunday!"

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

Use $(...) to execute a command and return the output as a string:

[[ $(date '+%a') == "Sun" ]]

Upvotes: 6

Related Questions