Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

In Bash, an integer is a numeric value represented internally as a signed integer (typically 64-bit on modern systems) but stored and manipulated by default as a character string. Because Bash is weakly typed, a variable acts as an integer only when evaluated within an arithmetic context or when explicitly assigned an integer attribute.

Explicit Integer Declaration

Variables can be strictly typed as integers using the declare or typeset built-ins with the -i option. Once the integer attribute is set, any subsequent assignment to that variable automatically forces an arithmetic evaluation. When a non-numeric string is assigned to an integer-typed variable, Bash performs recursive evaluation, treating the string as a variable name. It does not automatically default to zero unless the referenced variable is unset or empty.
declare -i my_int
my_int=5+5      # Evaluates to 10, not the literal string "5+5"


# Recursive evaluation of strings in arithmetic contexts
string=42
my_int="string" # Evaluates to 42, as "string" is evaluated as a variable name
my_int="unset"  # Evaluates to 0, as the variable "unset" is empty/undefined

Arithmetic Evaluation Contexts

Bash processes strings as integers when they are placed inside specific arithmetic constructs. Variables referenced inside these contexts do not require the $ prefix for evaluation.
  • Arithmetic Expansion ($((...))): Evaluates the expression and replaces the construct with the resulting integer.
  • Arithmetic Command (((...))): Evaluates the expression and returns an exit status (0 if the mathematical result is non-zero, 1 if the result is zero).
  • The let Built-in: Evaluates arithmetic expressions passed as string arguments.

# Arithmetic expansion
result=$(( 10 * 2 ))


# Arithmetic command
(( result++ ))


# let built-in
let "result = result / 2"

Radix (Base) Representation

Bash integers support arbitrary bases from 2 to 64. The standard syntax for defining a base is base#number. Bash also recognizes standard C-style prefixes for octal and hexadecimal integers. To evaluate these representations as integers upon assignment, they must be wrapped in an arithmetic expansion or assigned to a strictly typed integer variable; otherwise, they are treated as literal strings.

# Using arithmetic expansion for base evaluation
bin_val=$(( 2#1010 ))      # Binary (Base 2) representation of 10
hex_val=$(( 16#FF ))       # Hexadecimal (Base 16) representation of 255
base64_val=$(( 64#@ ))     # Base 64 representation


# C-style prefixes
oct_val=$(( 0755 ))        # Leading '0' denotes Octal (Base 8)
hex_val_c=$(( 0xFF ))      # Leading '0x' or '0X' denotes Hexadecimal


# Using explicit integer declaration
declare -i typed_bin
typed_bin=2#1010           # Evaluates to 10 due to the integer attribute

Technical Characteristics and Limitations

  • Precision: Bash integers are fixed-width signed integers. On 64-bit systems, the range is -9223372036854775808 to 9223372036854775807.
  • Overflow: Bash does not throw exceptions on integer overflow or underflow. It silently wraps around the maximum or minimum values.
  • Floating-Point: Bash has no native support for floating-point arithmetic. Division operations strictly perform integer division, truncating any fractional remainder toward zero.

# Integer division truncation
echo $(( 10 / 3 ))  # Outputs 3


# Overflow wraparound (on 64-bit systems)
echo $(( 9223372036854775807 + 1 )) # Outputs -9223372036854775808
Master Bash with Deep Grasping Methodology!Learn More