${(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 是一款支持 W** 的 **Zsh 集成开发环境 - 立即试用
© 2020–2024 Joachim Ansorg
声明
隐私政策
许可条款