October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Bash Parameter Expansion: What to Know About Substrings and Replacements

A negative substring offset is not the same as Bash’s default-value operator, and an unquoted ampersand can represent matched text when patsub_replacement is enabled.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The 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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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 of patsub_replacement and 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.