Skip to main content
The % operator in Bash serves three distinct syntactic roles depending on its evaluation context: an arithmetic modulo operator, a parameter expansion operator for suffix removal, and a job control specifier.

Arithmetic Modulo Operator

Within an arithmetic evaluation context, % functions as the modulo operator. It evaluates the remainder of integer division between two operands. Because Bash natively supports only integer arithmetic, floating-point operands will result in a syntax error. Syntax:
Mechanics:
  • Evaluated exclusively inside Bash’s internal arithmetic contexts ($(( )), (( )), or let).
  • The sign of the result matches the sign of the dividend.
  • Division by zero throws a division by 0 runtime error.
Example:

Parameter Expansion (Suffix Pattern Removal)

Within parameter expansion, % acts as a pattern-matching operator that strips a specified suffix from a variable’s string value. It uses standard shell globbing rules (not regular expressions) to match the pattern against the end of the string. Syntax:
Mechanics:
  • Single % (Non-greedy): Deletes the shortest possible match of the pattern from the right side (suffix) of the expanded $variable.
  • Double %% (Greedy): Deletes the longest possible match of the pattern from the right side of the expanded $variable.
  • If the pattern does not match the suffix, the original string is returned unmodified.
Example:

Job Control Specifier

In an interactive shell environment (or scripts where set -m is enabled), % is used as a prefix to reference background or suspended jobs by their job ID or command string, rather than their Process ID (PID). Syntax:
Example:
Tired of Poor Bash Skills? Fix That With Deep Grasping!Learn More