Two Bash parameter-expansion details can make a reviewed script behave differently than expected: a space changes how a negative number is parsed in a substring expansion, and an unquoted ampersand can stand for the matched text during replacement. The behavior depends on the expansion form and, for ampersands, the patsub_replacement shell option.
Why does ${value: -2} differ from ${value:-2}?
They use different operators. In scalar substring expansion, ${parameter:offset:length}, a negative offset counts backward from the end of the value. The space before -2 makes the negative offset unambiguous:
As an Amazon Associate I earn from qualifying purchases.
value=abcdef
printf '<%s>n' "${value: -2}"
This prints <ef>. By contrast, ${parameter:-word} is the default-value operator: it expands to word when the parameter is unset or null. Thus ${value:-2} is not a request for the final two characters; with value=abcdef, it expands to abcdef.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsThe GNU Bash Reference Manual 5.3 documents negative scalar offsets as character offsets counted from the end of the parameter value. The manual’s current edition is dated 18 May 2025. Read the Shell Parameter Expansion section; the manual index identifies its version and edition.
#1 Best Overall
- Used Book in Good Condition
Does a negative length also count backward?
For a scalar substring expansion, yes, but it means something different from a negative offset. A negative offset chooses a starting position from the end; a negative length specifies an endpoint position from the end. Bash expands the characters between the starting offset and that endpoint.
value=abcdef
printf '<%s>n' "${value:1:-1}"
Here the start is character position 1 and the endpoint is one character back from the end, so the result is bcde.
Do not carry that scalar rule over to every operand type. A negative length is an expansion error when the substring operand is positional parameters or an indexed array. This is distinct from a negative offset, which is documented as counting from the end.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Why can an ampersand become the matched text?
In Bash pattern substitution, ${parameter/pattern/string} replaces the first match, while ${parameter//pattern/string} replaces every match. The pattern is a shell pattern, not a regular expression. In Bash 5.3, when the patsub_replacement shell option is enabled, an unquoted & in the replacement string stands for the portion matched by the pattern.
shopt -s patsub_replacement
value=cat cat
printf '%sn' "${value/cat/&!}"
printf '%sn' "${value/cat/&!}"
With the option enabled, the first line replaces the first cat with the matched text followed by !, producing cat! cat. Quoting the ampersand in the replacement makes it literal, so the second line produces &! cat. To see the global form, change the substitution operator from / to //; both matching instances are then replaced.
The special meaning is conditional: with patsub_replacement disabled, the ampersand does not acquire the matched-text meaning described above. The option can be checked or changed with shopt -q patsub_replacement, shopt -s patsub_replacement, and shopt -u patsub_replacement. Do not assume a default across all Bash versions, distributions, or builds; inspect the shell in which the script runs.
Quick Recap
Best Value
Rank #4
What to check in a code review
- For a negative scalar offset, retain the separating space, as in
${value: -2}, and verify the intended starting point. - For a negative substring length, confirm the operand is scalar; positional parameters and indexed arrays treat it as an expansion error.
- Check whether replacement should affect only the first match (
/) or every match (//). - When a replacement contains
&, check the runtime value ofpatsub_replacementand whether the replacement character is quoted. - Remember that the pattern uses Bash shell-pattern syntax, not regular-expression syntax.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




