[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Are declare and typeset exactly the same? Which one is m
Re: [Help-bash] Are declare and typeset exactly the same? Which one is more popular among all shells?
Sun, 4 Aug 2019 11:03:54 +0100
2019-08-03 20:28:22 -0500, Peng Yu:
> man bash says the following. I assume this means that declare and
> typeset are exactly the same in bash. Is it so?
> declare [-aAfFgilnrtux] [-p] [name[=value] ...]
> typeset [-aAfFgilnrtux] [-p] [name[=value] ...]
> ksh only has typeset. Is typeset more popular among all the
> contemporary shells than declare? Thanks.
"typeset" is the ksh builtin/keyword added by ksh in the early
80s. It was added to zsh in 2.0 in 1991, with "declare" as an
alias for bash compatibility, and in yash in 2.0 as well
bash chose declare instead in 1989 but has always supported
typeset as an alternative for ksh compatibility.
ash (1989 as well) has neither but has "local" to make a
variable local in a function.
So, typeset is available wherever declare is, but the contrary
is not true (there's no "declare" in pdksh/mksh/ksh93/yash).
local was added to yash in 2018. It is also supported by pdksh
and derivatives, but not ksh88 or ksh93.
ksh88 ash pdksh bash zsh ksh93 mksh yash
typeset Y N Y Y Y Y Y Y
declare N N N Y Y N N N
local N Y Y Y Y N Y Y
(beware there are differences between implementations on how
local/typeset are handled and even how scoping is done)
The POSIX sh specification has none of the 3 but there's an
ongoing effort to specify some sort of "local" builtin/keyword.