How to get a variables datatype in zsh

variablezsh

For example zsh uses the variable $fpath to define include directories.
How can I tell what type of variable $fpath is?
i.e. is fpath a

  • string
  • number
  • numeric array
  • associative array

Best Answer

You can use t parameter expansion flag:

$ print -rl -- ${(t)fpath}
array-special
$ a=1
$ print -rl -- ${(t)a}
scalar
$ a=(1 2)
$ print -rl -- ${(t)a}
array
$ typeset -A a
$ print -rl -- ${(t)a}
association

Note that you can't distinguish between array of integers or array of strings.