19

As answered in Highlight the current date in cal

the current date in output form cal is automatically highlighted (reverse colors) if the output goes to terminal.

That's what I had always been getting.

However, with my current Debian GNU/Linux, it is not the case any more, and I'm wondering what the fix is.

enter image description here

$ echo $TERM
xterm

$ lsb_release -a 
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux bullseye/sid
Release:        testing
Codename:       bullseye
5
  • 5
    The answer that you appear to be quoting was based on OpenSUSE, and the comment below it notes that there are several different implementations of cal. In particular, Debian based systems include ncal which may be what you have - in which case ncal -b or ncal -C should give you traditional cal layout plus highlighting. Commented Aug 11, 2021 at 22:00
  • 3
    It's one of several known bugs. On Debian cal==ncal Commented Aug 11, 2021 at 22:19
  • 1
    @ThomasDickey in this case though, it's as per design, not a bug. Commented Aug 12, 2021 at 6:35
  • 2
    not really: the manpage isn't "updated" to match the regression. Commented Aug 12, 2021 at 7:42
  • @ThomasDickey, while I agree the manual could be improved, there's nothing in there that says that the current day is highlighted in the output of cal. The synopsis for cal clearly shows that -h is not one of the valid options. Commented Aug 12, 2021 at 8:58

3 Answers 3

18

I believe the correct "Answer" to this question is documented here on GitHub

To quote add

alias cal="if [ -t 1 ] ; then ncal -b ; else /usr/bin/cal ; fi"

into your shell rc file.

This is an extremely irritating change. Changing the behavior of a frequently used cli command for at least 17 years to make it "correct" is kind of insane. Now I understand why so many people hate Windows so much but are still reluctant to switch to Linux. I'm pretty sure almost all package maintainer who use cal (actually I think majority of them uses date anyway) are trained to use cal -h to turn off the highlight. Now the change even breaks compatibility with cal -h.

The change is documented here

A simpler hack to solve the "no highlight" is to alias cal to ncal -b, but it is not 100% correct with the package ncal maintainer's expectation.

6
  • Note that although it is a Debian problem and I've accepted and upvoted this Debian-specific solution, but I was seeking for a more universal solution so I'd prefer the answer from unix.stackexchange.com/a/185230/374303 myself instead. Commented Sep 12, 2021 at 15:45
  • @xpt I tried the solution proposed, but if the day of month is 1 or 2 there will be multiple 1x and 2x days get highlighted. Not sure if it is just me. The grep color is indeed quite good effect though. Commented Sep 13, 2021 at 16:58
  • 1
    ~~which command did you try? Make sure it is "\b$(date +%e | sed "s/ //g")", i.e., with the sed part~~. Oh, I got you, there is an obvious flaw in that answer (I fixed it on my end without thinking much to it). Check my comment I just put there. Commented Sep 13, 2021 at 20:19
  • 2
    what does [ -t 1 ] is doing there? Commented Aug 24, 2022 at 5:50
  • 2
    @AmadoC it is unix voodoo magic. -t FD test if file descriptor (FD) is a file descriptor that is associated with a terminal. So basically it is checking if the shell is running in interactive mode or scripting mode (though not technically perfect in this explanation). In this case, it is testing stdout (fd 1). Feel free to replace it with stderr (2) or stdin (0). Commented Oct 20, 2022 at 14:17
4

cal -3 doesn't work after alias, this accepts options:

if [ -t 1 ] ; then alias cal="ncal -b" ; else alias cal="/usr/bin/cal" ; fi
1
  • Thx. Welcome aboard with my +25! Commented Dec 14, 2022 at 1:37
2

I just wanted to add onto @ShiB's great answer with the following:

function cal() {
    if [ -t 1 ]; then ncal -b "${@}"; else command cal "${@}"; fi
}

Changes from @ShiB's answer:

  1. Use a function instead of an alias to facilitate passing arguments to the command(s)
  2. Replace /usr/bin/cal with command cal in case the user has cal installed in a non-standard location

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.