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.

The % character in Bash serves three distinct syntactic roles depending on the parsing context: an arithmetic modulo operator, a parameter expansion operator for suffix manipulation, and a job control identifier (jobspec).

Arithmetic Modulo Operator

Within an arithmetic expansion context, % functions as the modulo operator, evaluating the remainder of an integer division between two operands. The compound assignment operator %= divides a variable by a value and assigns the remainder back to the variable.
$(( operand1 % operand2 ))
(( operand1 %= operand2 ))
let "result = operand1 % operand2"
  • Integer Restriction: Bash arithmetic evaluates strictly as integers. Passing floating-point numbers to the % operator results in a syntax error.
  • Evaluation Order: Modulo shares the same precedence level as multiplication (*) and division (/), evaluated left-to-right before addition and subtraction.
  • Zero Divisor: If the right operand evaluates to 0, Bash throws a division by 0 error. This aborts the specific arithmetic evaluation and returns a non-zero exit status, but it is a standard error that does not terminate the script or shell session unless the errexit option (set -e) is active.

Parameter Expansion (Suffix Manipulation)

Within parameter expansion, % acts as a pattern-matching operator that either strips a specified suffix from the expanded value of a variable or anchors a search-and-replace operation to the end of the value.
${variable%pattern}               # Suffix removal: Non-greedy (shortest) match
${variable%%pattern}              # Suffix removal: Greedy (longest) match
${variable/%pattern/replacement}  # Search and replace: Anchored to suffix
  • Pattern Matching: The pattern is evaluated using standard shell globbing rules (e.g., *, ?, [...]), not regular expressions.
  • Anchoring: The match is strictly anchored to the end (suffix) of the string. If the pattern matches a substring in the middle of the variable but not the end, no removal or replacement occurs. In a search-and-replace expansion, placing % immediately before the pattern explicitly enforces this suffix anchoring.
  • Greediness: For suffix removal, a single % performs a non-greedy match, removing the shortest possible suffix that satisfies the pattern. A double %% performs a greedy match, removing the longest possible suffix.
  • Immutability and Expansion: This operation does not mutate the original variable in memory. Instead, it performs text substitution directly into the command line during the shell’s expansion phase.

Job Control Identifier

In an interactive shell environment, % is a reserved character used to construct a jobspec, referencing background or suspended jobs managed by the shell’s internal job table.
%n        # References job number 'n'
%+ or %%  # References the current (most recently foregrounded/backgrounded) job
%-        # References the previous job
%string   # References a job whose command line begins with 'string'
%?string  # References a job whose command line contains 'string'
%n        # Standalone execution (equivalent to 'fg %n')
  • Context: This syntax is recognized as an argument by job control builtins such as fg, bg, kill, disown, and wait.
  • Standalone Execution: A jobspec can be executed as a standalone command. When a command line begins with a jobspec (e.g., %1), Bash treats it as a built-in shorthand for bringing that specific job to the foreground. Executing %1 is strictly equivalent to executing fg %1.
  • Resolution: The shell does not expand jobspecs into Process IDs (PIDs) during standard command-line parsing. Instead, the literal jobspec string is passed as an argument to the builtin command. The builtin itself parses the string to look up and interact with the corresponding entry in the shell’s internal job table.
Master Bash with Deep Grasping Methodology!Learn More