Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: :s modifier right-hand-side value behaves strangely with a variable



On Sun, Aug 23, 2026 at 7:29 AM Jonathan Poelen
<jonathan.poelen@xxxxxxxxx> wrote:
>
> When a variable is used in the replacement part of the :s/l/r/ modifier,
> the result depends on various obscure conditions, which leads to a truncated
> replacement and a found string that is longer than the search string.

You're making an incorrect assumption about evaluation order.

str='abc,abc'
echo $str:s/b/$rep

results in 'a$repc,abc' which then expands as "a${repc},abc" because
without braces the $varname parse consumes everything that looks like
part of an identifier, stopping when it reaches the comma.

If you put braces around ${rep} you get "a${rep}c,abc" which is the
result you expected.




Messages sorted by: Reverse Date, Date, Thread, Author