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.

A Bash numeric base literal is a syntactic construct used within arithmetic evaluation contexts to define an integer in an arbitrary radix (base) ranging from 2 to 64.

Syntax

The primary syntax for defining a base literal utilizes the hash (#) delimiter:
base#number
  • base: A decimal integer between 2 and 64 specifying the radix.
  • number: The sequence of characters representing the integer value in the specified radix.
If the base# prefix is omitted entirely, Bash evaluates the number as a standard base 10 integer (unless a C-style prefix is present). The # delimiter is strictly a separator and cannot be used without a preceding base.

Lexical Representation and Character Mapping

To represent digit values greater than 9, Bash employs a specific character mapping sequence:
  • Values 0–9: Digits 0 through 9.
  • Values 10–35: Lowercase letters a through z.
  • Values 36–61: Uppercase letters A through Z.
  • Value 62: The at-symbol @.
  • Value 63: The underscore _.
Note: If the specified base is less than or equal to 36, Bash treats lowercase and uppercase letters interchangeably for values 10 through 35.

C-Style Base Prefixes

In addition to the base#number format, Bash supports standard C-style prefixes for octal and hexadecimal literals:
  • Octal (Base 8): A leading 0 (zero) followed by octal digits.
  • Hexadecimal (Base 16): A leading 0x or 0X followed by hexadecimal digits.

Arithmetic Evaluation Contexts

Bash evaluates numeric base literals in all arithmetic evaluation contexts. These contexts include:
  • Arithmetic expansions ($(( ... )))
  • Arithmetic compound commands ((( ... )))
  • The let builtin command
  • Array subscripts (array[index])
  • Parameter substring expansion offsets and lengths (${parameter:offset:length})
  • Integer-attributed variable assignments (variables declared with declare -i)
  • Conditional command arithmetic operators (e.g., -eq, -lt) evaluated strictly within the [[ ... ]] keyword.
Note: The [ (test) builtin command is not an arithmetic evaluation context. It relies on standard C-library string-to-integer conversion. Using a Bash base literal inside [ (e.g., [ 16#FF -eq 255 ]) will result in an integer expression expected error.

Syntax Visualization


# Arithmetic expansions and compounds
$(( 2#1010 ))              # Binary: Evaluates to 10
$(( 8#17 ))                # Octal: Evaluates to 15
$(( 017 ))                 # Octal (C-style): Evaluates to 15
$(( 16#FF ))               # Hexadecimal: Evaluates to 255
$(( 16#ff ))               # Hexadecimal: Evaluates to 255 (Case-insensitive <= base 36)
$(( 0xFF ))                # Hexadecimal (C-style): Evaluates to 255
$(( 64#1_ ))               # Base 64: Evaluates to 127 (1 * 64^1 + 63 * 64^0)
$(( 64#@0 ))               # Base 64: Evaluates to 3968 (62 * 64^1 + 0 * 64^0)


# Array subscripts
arr[16#FF]="value"         # Evaluates index as 255


# Substring expansion
${var:16#A:2#10}           # Evaluates offset as 10, length as 2


# Integer-attributed variables
declare -i num=16#FF       # Evaluates literal and assigns 255 to num


# Conditional arithmetic operators (Must use [[ ]], not [ ])
[[ 16#FF -eq 255 ]]        # Evaluates to true (0 exit status)
Master Bash with Deep Grasping Methodology!Learn More