16

If I write the following into a bash terminal:

A="0012"
B=$((A+1))
echo $B

I get 11, Not 13 as I expected!!!!!

I have googled around and I can't explain it at all, or work out how I can get the number incremented. (I actually want to end up with B="0013" and increment one each time as I am using this as the prefix to my backups)

  • 2
    Beware of leading zeros on essentially any language originating on UNIX. This normally means octal. – Joshua Sep 11 '16 at 22:33
  • No you are not you get 1011 binary – Ken Mollerup Sep 28 '16 at 11:50
28

That's because numbers starting with 0 are treated as Octal by bash, hence it is doing Octal (Base 8) addition. To get Decimal addition for this structure, you need to explicitly define the Base or not use 00 altogether.

For Decimal, the Base is 10, denoted by 10#:

$ A="10#0012"
$ echo $((A+1))
13
5

You may try this command to get the answer:

A="0012"
echo $A + 1 | bc

More information about bc command can be found here.

bc man page:

NAME
       bc - An arbitrary precision calculator language

SYNTAX
       bc [ -hlwsqv ] [long-options] [  file ... ]

DESCRIPTION
       bc is a language that supports arbitrary precision numbers with interactive execution of statements.  There are some similarities
       in the syntax to the C programming language.  A standard math library is available by command line  option.   If  requested,  the
       math  library is defined before processing any files.  bc starts by processing code from all the files listed on the command line
       in the order listed.  After all files have been processed, bc reads from the standard input.  All code is executed as it is read.
       (If a file contains a command to halt the processor, bc will never read from the standard input.)

       This  version of bc contains several extensions beyond traditional bc implementations and the POSIX draft standard.  Command line
       options can cause these extensions to print a warning or to be rejected.  This document describes the language accepted  by  this
       processor.  Extensions will be identified as such.
  • 4
    Instead of using echo and a pipe, you could use Bash's "here string" syntax. The effect is the same, but IMHO the "here string" is more beautiful: bc <<< "$A + 1" :-) – Byte Commander Sep 11 '16 at 14:26
  • A one or two sentence introduction of the bc command in addition to a here link would be helpful. – WinEunuuchs2Unix Sep 11 '16 at 14:41
2

An alternate method may be to keep your variables as integers and convert them to a string at the end:

A=12
B=$((A+1))
echo $B
13
C=$( printf '%04d' $B )
echo $C
0013

This style of working with integers in math and converting to string for the answer is more intuitive to me as I'm used to BASIC programming. I appreciate Bash doesn't have variable typing like C and BASIC but pretending it does makes me happy.

  • This was a test to highlight the issue I was having. I read the initial variable by taking the output of another command which is text and has leading zeros. – Robert3452 Sep 11 '16 at 14:46
  • Ah... history always explains how we got to the present. – WinEunuuchs2Unix Sep 11 '16 at 14:50
  • @Robert3452 You could also strip leading zeroes: A="0012"; A=$((10#$A)) – wjandrea Sep 17 '16 at 3:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.