Bases#

The result of any Bash arithmetic is a string representing a base 10 integer, but the inputs to an arithmetic expression can be in any of several different formats. Base 10 is the most obvious, consisting of only numbers zero through nine.

Octal numbers (base 8) start with a zero followed by numbers zero through seven. For example, 035 means “three eights plus five,” which in decimal is 29. Or as a Bash expression, (( 035 == 29 )). This is convenient in a few areas to express a collection of three bits (eight unique values) per character, such as file permissions.

Hexadecimal numbers (base 16) start with 0x followed by any one of zero through nine or the first six letters of the alphabet to represent ten through fifteen. One common number you might see is 0xff, which represents the maximum value of a single byte, 255 or 2^8^ – 1. So a single letter in hexadecimal represents half a byte, also known as a “nibble.” Who said programmers don’t know how to have fun?

 

This page is a preview of The newline Guide to Bash Scripting

No discussions yet