Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-05-2012, 11:22 PM   #1
LQ Newbie
Registered: Mar 2012
Location: Kerala, India
Distribution: Ubuntu
Posts: 2

Rep: Reputation: Disabled
Unhappy problem with bash calculation

why is
echo $((1111111111111111111111111%10))
and not 1

I was writing a blog on Fibonacci series ( ) and above problem was only solved by
echo "1111111111111111111111111%10" | bc

Last edited by dileepmani; 04-05-2012 at 11:30 PM.
Old 04-06-2012, 12:04 AM   #2
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
There are limits to the values that can be used in bash--as with every other program.

The bash man page states:
       The  shell allows arithmetic expressions to be evaluated, under certain
       circumstances (see the let and declare builtin commands and  Arithmetic
       Expansion).   Evaluation  is done in fixed-width integers with no check
       for overflow, though division by 0 is trapped and flagged as an  error.
I did not see a reference to the exact size of integer bash supports.

However, integers are typically 32 or 64 bits wide. In either case, your string of ones exceeds the value capable of being represented by either. The maximum value a 64-bit integer can represent (without being concerned with negative values) is 18,446,744,073,709,551,615.

For comparison:
your string of ones:             1,111,111,111,111,111,111,111,111
maximum unsigned 64-bit integer:        18,446,744,073,709,551,615
Your value very likely exceeds the maximum value bash is capable of handling, bash does not signal an "overflow" when you try to use it (as mentioned in the man page above), and so, the number of bits needed to represent your value that exceed bash's internal storage are stripped/dropped/truncated. Your script would be left with a number completely different than you would expect.

The bc program likely uses extra code and/or larger data sizes that can accommodate your value.

Note: the maximum value I showed for a 64-bit integer above is for an unsigned integer. For an integer that can represent both positive and negative values, the maximum value would be reduced by approximately half.

After further research and experimentation, on my machine, the maximum range of integer values bash can represent (on my machine):
[-9,223,372,036,854,775,808  <-->  +9,223,372,036,854,775,807]
Which corresponds to a signed 64-bit integer.

Shell session:
user@localhost$ limitcheck=9223372036854775807
user@localhost$ echo $limitcheck
user@localhost$ let limitcheck=limitcheck+1
user@localhost$ echo $limitcheck

Last edited by Dark_Helmet; 04-06-2012 at 12:33 AM.
Old 04-06-2012, 12:34 AM   #3
Registered: Nov 2011
Location: Germany, Bavaria, Nueremberg area
Distribution: openSUSE, Debian, LFS
Posts: 205

Rep: Reputation: 43
bash uses long.
It does NOT check for overflows and wraps around starting then with LLONG_MIN.

So if you do let the bash calculate numbers greater than a LONG, it will give wrong results.

In the second case the bash did no calculating at all.
It just gave a string to bc.
Old 04-06-2012, 06:13 AM   #4
LQ Newbie
Registered: Mar 2012
Location: Kerala, India
Distribution: Ubuntu
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks a lot. Your explanations are very lucid.

Last edited by dileepmani; 04-06-2012 at 06:15 AM.
Old 04-06-2012, 02:29 PM   #5
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,566

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
Please mark as SOLVED once you have a solution.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 06:52 AM
[SOLVED] useradd problem with UID calculation rylphs Linux - Newbie 5 08-30-2010 04:48 PM
[SOLVED] BASH: Download Script -> While Loop Calculation Toubles... Devcon Programming 5 07-29-2010 02:41 AM
problem with process time calculation aloktiagi Linux - Newbie 3 08-11-2009 01:05 PM
Simple bash calculation Hiperi0n Programming 12 11-14-2008 09:10 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:33 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration