3

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?

1 Answer 1

7

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.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .