${(w)…}: Word count

w used together with #name counts words in arrays or string

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

(W) can be used instead if empty words should be counted, too.

Examples

Count words
1
2
3
4
string="string with several words"
array=(array "with several" words)
echo ${(w)#string}
echo ${(w)#array}
Output:
1
2
4
4
BashSupport Pro is a Zsh IDE with support for ${(w)…}try it now!
© 2020–2024 Joachim Ansorg
Imprint
Privacy Policy
Legal