Unix/Linux code to test whether “today” is a weekend day (or weekday)

If you ever need an example of a Unix/Linux shell script where you need to determine whether today is a weekend day, I can confirm that this code works:

# like "SAT" or "SUN"
dayName=`date +%a | tr '[:lower:]' '[:upper:]'`

if [ $dayName = "SAT" -o $dayName = "SUN" ]
then
    echo "WEEKEND"
else
    echo "other"
fi

That code results in dayName being something like SAT or SUN, so the if/then test prints whether “today” is a weekend day or not. The tr command converts the output from the date command to uppercase. That isn’t completely necessary, but if you don’t use it, you have to test again the strings Sat or Sun, so I prefer it in this case. This code will work under both the Bourne and BASH shells.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.