`cal -h` command: Bash substring `${string:offset:length}` error

Your problem is that cal is already highlighting the current day, and the control codes are throwing off the offsets

Look at /tmp/terminal in a hex editor. Today (the 11th) is: 5F 08 31 5F 08 31, and not 31 31

Use cal -h to switch off the auto highlighting of today's date.


@Martin Thornton would be OK if all implementations of cal actually recognised the -h as switching off formatting. Debian doesn't even though man suggests it does. It displays the usage instead. So this would work.

cal > /tmp/terminal1
tr -cd '\11\12\15\40\60-\136\140-\176' < /tmp/terminal1  > /tmp/terminal

The file gets the cal output with formatting and the next line removes anything we don't want and lets the rest of the code do its work.