${(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.
Links
Examples
- Count words, including empty words
- Output:
1 2 3
string="string-with---several-words" echo "With empty words: ${(Ws:-:)#string}" echo "Without empty words: ${(ws:-:)#string}"
1 2
With empty words: 6 Without empty words: 4
BashSupport Pro is a Zsh IDE with support for ${(W)…} – try it now!