Re: ${!nameref:offset:length} behaves inconsistently for scalar vs array nameref targets

Chet Ramey <[email protected]> Mon, 11 May 2026 12:14:57 -0400
Newsgroups gmane.comp.shells.bash.bugs
Organization ITS, Case Western Reserve University
Message-ID <[email protected]>
On 5/9/26 10:33 PM, Emerald YourLi wrote:

> Bash Version: 5.3
> Patch Level: 0
> Release Status: release

>   - Array target: ${!nameref:0:n} silently SKIPS the indirect
>     expansion entirely and instead behaves as if the expression were
>     ${array[0]:0:n} (i.e., nameref is transparently resolved, the
>     first array element is returned, and substring is applied to it).

Nothing is skipped. Bash performs the nameref expansion, resulting in

${arr:0:100}.

Nameref expansion applies to the `parameter' part of the word expansion,
which in this case is a variable name.

Since the array variable is used without a subscript, it's equivalent to
arr[0].

You would see this if you tested with

${arr:0:100}.

>   This inconsistency is not documented anywhere in the Bash manual.

The Arrays section says:

"Referencing an array variable without a subscript is equivalent to ref-
  erencing the array with a subscript of 0."

>   The manual section on indirect expansion (Parameter Expansion) does
>   not mention any distinction based on the type of the referenced
>   variable.

"If parameter is a nameref, this expands to the
  name of the parameter referenced by parameter"

The rest follows from how an array variable is expanded.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    [email protected]    http://tiswww.cwru.edu/~chet/
OpenPGP_signature.asc (application/pgp-signature, 203 B)
-----BEGIN PGP SIGNATURE-----

wmMEABEIACMWIQR8ATX7CIqvbGbGULm7WGnwZOp0qwUCagIAgQUDAAAAAAAKCRC7WGnwZOp0q9LN
AJwNoMMGn/yf8Q7I4bwSaL92Ja4PawCeKQlVU8tj3X99aZeF/L1mBz8NB40=
=plt6
-----END PGP SIGNATURE-----