${(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 ist eine Zsh IDE mit Unterstützung für ${(W)…} - probieren Sie es jetzt aus!
© 2020–2024 Joachim Ansorg
Impressum
Datenschutz
Rechtliches