${(S)…}: Closest and non-greedy matching

(S) is only useful with parameter expansion operators ${...#...}, ${...%...}, and ${.../...}.

${(S)...#...}
${(S)...##...}
Search for the match that starts closest to the start of the string
${(S)...%...}
${(S)...%%...}
Search for the match that starts closest to the end of the string
${(S).../...}
${(S)...//...}
Non-greedy matching to replace the shortest instead of the longest match.

Examples

Non-greedy string replace
1
2
3
value="Monday is a working day"
echo Shortest match: ${(S)value//*day/...day}
echo Longest match: ${value//*day/...day}
Output:
1
2
Shortest match: ...day...day
Longest match: ...day
BashSupport Pro is a Zsh IDE with support for ${(S)…}try it now!
© 2020–2024 Joachim Ansorg
Imprint
Privacy Policy
Legal