Linux cal Command
What is the cal command
cal is a simple but practical command-line tool in Linux used to display a calendar in the terminal. Its name comes from the abbreviation of "calendar".
Basic Functionality
- Display the calendar for the current month
- Display the calendar for a specified month
- Display the calendar for an entire year
- Supports multiple display formats and options
Basic Syntax
cal [选项] [[月份] 年份]
Parameter Description
- Without any arguments: display the calendar for the current month
- Specifying only the year: display the calendar for all months of that year
- Specifying both month and year: display the calendar for that month
Details of common options
1. Display the current month's calendar (default)
Example
cal
Example output:
十月 2023
日 一 二 三 四 五 六
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
2. Display the calendar for a specified month
Example
cal
12
2023
Display the calendar for December 2023
3. Display the calendar for an entire year
Example
cal
2023
Display the calendar for all months of 2023
4. Display the previous/next month's calendar
Example
cal
-3
Display the current month plus one month before and after (three months in total)
5. Display the Julian calendar (1-366)
Example
cal
-j
Display the calendar in Julian date format, counting from day 1 to day 365/366 each year
6. Display Monday as the first day of the week
Example
cal
-m
By default, Sunday is the first day of the week; use -m to change it to Monday
Advanced usage
1. Combining options
Example
cal
-j
-m
12
2023
Display the Julian calendar for December 2023 with Monday as the first day of the week
2. Display the calendar for a specific year
Example
cal
1752
Display the calendar for 1752 (the year when the calendar historically switched from the Julian to the Gregorian system)
3. Display historical calendars
Example
cal
9
1752
Display the calendar for September 1752 (note that this month is missing 11 days)
Practical Application Examples
Example 1: Quickly view next month
Example
cal
$
(
date
"%m %Y"
-d
"next month"
)
Example 2: View the current quarter
Example
month
=$
(
date
%
m
)
cal
-3
$
(
(
(
month-
1
)
/
3
3
1
)
)
$
(
date
%
Y
)
Example 3: Create a calendar file
Example
cal
2023
2023
_calendar.txt
Save the 2023 calendar to a file
Frequently Asked Questions
Q1: Is the cal command displaying inaccurate results?
A: Make sure the year entered is in the range 1-9999 and the month is in the range 1-12
Q2: How do you display a calendar in another language?
A: You can set the LANG environment variable, for example:
Example
LANG
=zh_CN.UTF-
8
cal
Q3: What is the difference between cal and ncal?
A: ncal is a variant of cal that offers different layout options and can be man ncal View details
Summary table
| Option | Description | Example |
|---|---|---|
| None | Display the current month's calendar | cal |
| -j | Julian calendar format | cal -j |
| -m | Monday as the first day of the week | cal -m |
| -3 | Display the previous/current/next three months | cal -3 |
| -y | Display all months of the current year | cal -y |
| Month Year | Display the calendar for a specified month | cal 12 2023 |
| Year | Display the calendar for a specified year | cal 2023 |
Linux Command Reference