What is the difference between declare and typeset
In bash
, typeset
and declare
are exactly the same. The only difference is that typeset
is considered obsolete.
typeset: typeset [-aAfFgilrtux] [-p] name[=value] ...
Set variable values and attributes.
Obsolete. See `help declare'.
The man page even lists them in the same breath:
declare [-aAfFgilrtux] [-p] [name[=value] ...]
typeset [-aAfFgilrtux] [-p] [name[=value] ...]
Declare variables and/or give them attributes.
typeset
is portable to some other shells, for example, ksh93
. If you are aiming for cross-shell portability, use typeset
(and make sure that the way you are calling it is portable). If you don't care about such portability, use declare
.
I know a case where declare
is useful to avoid the evil eval
: variable indirection :
$ var=foo
$ x=var
$ declare "$x=another_value"
$ echo $var
another_value