By default, bc interprets any alpha character as a 9

System:

  • Ubuntu 22.04.3 LTS
  • GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
  • bc 1.07.1

Observation:

Both ibase and obase are unset.

echo "A0" | bc
90
echo "B0" | bc
90
echo "X0" | bc
90

Question:
Why does bc interpret alpha characters as 9s by default? Why wouldn’t an error message be preferable here?

Asked By: yossi-matkal

||

From man bc on a GNU system (with GNU bc 1.07 or newer):

A simple expression is just a constant. bc converts constants into internal decimal numbers using the current input base, specified by the variable ibase. (There is an exception in functions.) The legal values of ibase are 2 through 36. (Bases greater than 16 are an extension.)
Assigning a value outside this range to ibase will result in a value of 2 or 36. Input numbers may contain the characters 0-9 and A-Z. (Note: They must be capitals. Lower case letters are variable names.) Single digit numbers always have the value of the digit regardless of the value of ibase. (i.e. A = 10.) For multi-digit numbers, bc changes all input digits greater or equal to ibase to the value of ibase-1. This makes the number ZZZ always be the largest 3 digit number of the input base.

(Emphasis mine.)

Answered By: choroba
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.