引數長度

# Length of a string
$ var='12345'
$ echo "${#var}"
5

請注意,它是字元數的長度,不一定與位元組數相同 (如 UTF-8 中大多數字符以多個位元組編碼),也不是字形/字形數 (其中一些是字元組合),也不一定與顯示寬度相同。

# Number of array elements
$ myarr=(1 2 3)
$ echo "${#myarr[@]}"
3

# Works for positional parameters as well
$ set -- 1 2 3 4
$ echo "${#@}"
4

# But more commonly (and portably to other shells), one would use
$ echo "$#"
4