${(W)…}: Word count, including empty words

W used together with #name counts words in arrays or string. Contrary to (w), it also counts empty words.

Flag (s) can be used to define the word delimiter.

Examples

Count words, including empty words
1
2
3
string="string-with---several-words"
echo "With empty words: ${(Ws:-:)#string}"
echo "Without empty words: ${(ws:-:)#string}"
Output:
1
2
With empty words: 6
Without empty words: 4
BashSupport Pro is a Zsh IDE with support for ${(W)…}try it now!
© 2020–2024 Joachim Ansorg
Imprint
Privacy Policy
Legal