cal
Overview
The cal
command displays a calendar for a specified month and year. It’s useful for date reference, scheduling, and quick date calculations.
Syntax
cal [options] [month] [year]
cal [options] [year]
Common Options
Option | Description |
---|---|
-1 |
Display single month (default) |
-3 |
Display previous, current, and next month |
-A num |
Display num months after |
-B num |
Display num months before |
-y |
Display entire year |
-j |
Display Julian dates (day of year) |
-m |
Monday as first day of week |
-s |
Sunday as first day of week (default) |
-w |
Display week numbers |
--color |
Colorize output |
Key Use Cases
- Quick date reference
- Planning and scheduling
- Date calculations
- Historical date lookup
- Script date validation
Examples with Explanations
Example 1: Current Month
cal
Displays calendar for current month
Example 2: Specific Month and Year
cal 12 2024
Shows December 2024 calendar
Example 3: Entire Year
cal -y 2024
Displays full year 2024 calendar
Example 4: Three Month View
cal -3
Shows previous, current, and next month
Date Range Display
Show months after current:
cal -A 3 # Next 3 months
Show months before current:
cal -B 2 # Previous 2 months
Combine before and after:
cal -B 1 -A 1 # Previous, current, next
Julian Calendar
Show day of year:
cal -j
Julian date for specific month:
cal -j 3 2024 # March 2024 with day numbers
Week Display Options
Monday as first day:
cal -m
Show week numbers:
cal -w
Combine options:
cal -mw # Monday first + week numbers
Historical Dates
Historical calendar:
cal 9 1752 # September 1752 (calendar reform)
Ancient dates:
cal 1 1 # January year 1
Future dates:
cal 12 2050 # December 2050
Performance Analysis
- Very fast operation
- Minimal resource usage
- No network dependencies
- Good for scripting
- Efficient date calculations
Best Practices
- Use for quick date reference
- Combine with date command
- Consider locale settings
- Use Julian dates for day counting
- Helpful for scheduling scripts
Scripting Applications
Date validation:
#!/bin/bash validate_date() { local month=$1 year=$2 if cal "$month" "$year" >/dev/null 2>&1; then echo "Valid date" return 0 else echo "Invalid date" return 1 fi }
Business day calculation:
count_weekdays() { local month=$1 year=$2 cal "$month" "$year" | grep -E '[0-9]' | \ tr ' ' '\n' | grep -E '^[0-9]+$' | wc -l }
Integration Examples
With date for context:
echo "Today is $(date +%A), $(date +%B) $(date +%d)" cal -3
Planning script:
echo "Current month schedule:" cal echo "" echo "Upcoming deadlines:" # Show project deadlines
Locale Considerations
Different locales affect:
- First day of week
- Month names
- Date formatting
Set locale:
LC_TIME=en_US.UTF-8 cal LC_TIME=de_DE.UTF-8 cal
Output Formatting
Pipe to other commands:
cal | grep -E '[0-9]' # Extract date lines
Count days in month:
cal 2 2024 | tail -1 | awk '{print $NF}'
Find specific day:
cal | grep -o '\b15\b' # Find 15th day
Calendar Calculations
Days in month:
days_in_month() { cal "$1" "$2" | awk 'NF {last=$NF} END {print last}' }
First day of month:
first_day_of_week() { cal "$1" "$2" | awk '/^[A-Z]/ {getline; print NF}' }
Troubleshooting
- Invalid month/year combinations
- Locale-specific formatting issues
- Terminal width limitations
- Historical calendar accuracy
- Leap year calculations
Advanced Usage
Custom formatting with ncal:
ncal -b # Brief format ncal -M # Monday first
Specific day highlighting:
cal | sed "s/$(date +%d)/[$(date +%d)]/"
Historical Context
Calendar reform (1752):
cal 9 1752 # Shows 11 missing days
Leap year examples:
cal 2 2000 # Leap year (divisible by 400) cal 2 1900 # Not leap year (divisible by 100, not 400)
Automation Examples
Monthly report header:
#!/bin/bash echo "Monthly Report - $(date +%B %Y)" echo "================================" cal echo ""
Schedule reminder:
# Show next month for planning NEXT_MONTH=$(date -d "next month" +%m) NEXT_YEAR=$(date -d "next month" +%Y) echo "Next month planning:" cal "$NEXT_MONTH" "$NEXT_YEAR"
Color Output
Modern cal versions support color:
cal --color=always
Environment variable:
export CAL_COLOR=always
Integration with Other Tools
With remind/calendar apps:
cal && echo "" && remind ~/.reminders
With task managers:
cal -3 && echo "" && task list