The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,053 other followers

bash: converting numbers to human readable SI or IEC units

Posted by jpluimers on 2019/12/03

Many unix tools that report sizes in bytes can convert them to either IEC or SI readable formats.

For I wrote about last week, I also wanted that kind of behaviour. So I did some research and came up with the code and test cases below.

Note that depending on the bitness of your system, bash integer numeric values are limited in size; see [WayBack] What is the maximum value of a numeric bash shell variable? – Super User.

So I wrote a small bash script for that too, which needed also gave me the opportunity to show how a  perpetual while loop as explained by [WayBack] bash – “while :” vs. “while true” – Stack Overflow.

Two things that always bite me with these short scripts are expressions (done through [WayBack]Arithmetic Expansion) and comparisons (through[WayBack] Other Comparison Operators).

The IEC suffixes contain one extra i to indicate binary and – next to the ISO notation that were already ISO defined – made it into the ISO 80000 standard since 2008. Here is a comparison list from [WayBackBinary prefix – Wikipedia:

Prefixes for multiples of
bits (bit) or bytes (B)
Value SI
1000 k kilo
10002 M mega
10003 G giga
10004 T tera
10005 P peta
10006 E exa
10007 Z zetta
10008 Y yotta
1024 Ki kibi K kilo
10242 Mi mebi M mega
10243 Gi gibi G giga
10244 Ti tebi
10245 Pi pebi
10246 Ei exbi
10247 Zi zebi
10248 Yi yobi

Most tools nowadays default to binary IEC suffixes for byte sizes, though disk manufacturers still use SI suffixes because, well then they appear bigger but aren’t. Just for comparison, look at the numbers from [WayBack] File size – Wikipedia and [WayBack] IEC and SI Size Notations – AN!Wiki where I got the test cases from:

base2 Notation

IEC Notation   Size
-------------- -------------------------------------------------
KiB = kibibyte (2^10  / 1,024 bytes)
MiB = mebibyte (2^20  / 1,048,576 bytes)
GiB = gibibyte (2^30  / 1,073,741,824 bytes)
TiB = tebibyte (2^40  / 1,099,511,627,776 bytes)
PiB = pebibyte (2^50  / 1,125,899,906,842,624 bytes)
EiB = exbibyte (2^60  / 1,152,921,504,606,846,976 bytes)
ZiB = zebibyte (2^70  / 1,180,591,620,717,411,303,424 bytes)
YiB = yebibyte (2^80  / 1,208,925,819,614,629,174,706,176 bytes)

base10 Notation

SI Notation    Size
-------------- -------------------------------------------------
KB = kilobyte  (10^3  / 1,000 bytes)
MB = megabyte  (10^6  / 1,000,000 bytes)
GB = gigabyte  (10^9  / 1,000,000,000 bytes)
TB = terabyte  (10^12 / 1,000,000,000,000 bytes)
PB = petabyte  (10^15 / 1,000,000,000,000,000 bytes)
EB = exabyte   (10^18 / 1,000,000,000,000,000,000 bytes)
ZB = zettabyte (10^21 / 1,000,000,000,000,000,000,000 bytes)
YB = yottabyte (10^24 / 1,000,000,000,000,000,000,000,000 bytes)



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: