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

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



Hello,

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.

This behavior occurs only with the variable expansions `$str:s...` and `${str:s...}`.
History expansion and globbing expansion do not have this problem.
When the HIST_SUBST_PATTERN option is enabled, the behavior is also correct.

Note: HIST_SUBST_PATTERN is not enabled by default.

```
str='abc,abc'
rep='_'

echo $str:s/b/_ # ok -> a_c,abc

# The forms `$s/l/r/`, `${s/l/r}`, and `${s/l/r/}` behave the same way

echo $str:s/b/$rep # NOK -> a,abc instead of a_c,abc
echo $str:s/b/:$rep # NOK -> a:,abc instead of a:_c,abc
echo $str:s/b/$rep$rep # NOK -> a_,abc instead of a__c,abc
echo $str:s/b/$rep. # ok -> a_.c,abc
echo $str:s/b/${rep} # ok -> a_c,abc
```

From what I've observed when a variable is used at the end:
- ‘b’ and everything following the pattern [_[:alnum:]] is removed
- the end variable is ignored
- what comes before the end variable is indeed used in the replacement
- everything works correctly if the variable uses braces

Another point: the documentation mentions the form `s/l/r/:G`,
but `:G` is only possible with history expansion.
I think this detail is missing.


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