49

When typing variables in Bash, what is the difference between declare and typeset? When used inside a function: what is the difference between declare and typeset and local?

The only difference I have come across is that typeset is portable to ksh scripts. Other than that, are there any reasons why one should be preferred over the other?

UPDATE: Added local to the question.

Keith Pinson
  • 7,162
  • 5
  • 54
  • 97
lecodesportif
  • 9,527
  • 8
  • 31
  • 53

3 Answers3

32
  • Difference between typeset and declare:

The former is more portable(e.g. ksh), while the latter is more preferable when portability is not a concern.

  • Difference between declare(or typeset) and local when used inside a function:

The former implies the latter, but more powerful. For example, declare -i x makes x have the integer attribute, declare -r x makes x readonly, etc.

Hui Zheng
  • 9,306
  • 2
  • 29
  • 37
  • 21
    `local` and `declare` are mostly identical and take all the same arguments with two exceptions: local will fail if not used within a function, and local with no args filters output to print only locals, declare doesn't. – ormaaj Apr 18 '12 at 04:02
  • @ormaaj you're right, I didn't realize that `local` supports options too. Thanks a lot. – Hui Zheng Apr 18 '12 at 05:28
  • so a variable declared with `typeset` or `declare` is *local*? seems like that question is the most obvious question to answer. – Alexander Mills Apr 21 '18 at 22:53
  • 1
    @AlexanderMills Using `declare` or `typeset` without the `-g` modifier inside a function will declare a local variable. `local` will of course also create a local variable. Any other method of declaring a variable inside a function will create a global variable. – Aethalides Aug 17 '18 at 13:45
  • I know that this is old, but it's useful to note that within a function, `declare` and `local` are identical (apart from using `local` without parameters). If you use `local -g`, the variable will counterintuitively be global! For me, the only useful distinction is visual, so that the human reader knows that `local` is intended to be local, whereas `declare` is intended to be used with `-g`. – Paddy Landau Jan 20 '21 at 12:10
13

As far as bash is concerned, no, there is no difference. In fact, the manpage has them share the same definition

declare [-aAfFilrtux] [-p] [name[=value] ...]
typeset [-aAfFilrtux] [-p] [name[=value] ...]
Declare variables and/or give them attributes. If no names are given then display the values of variables. The -p option will display the attributes and values of each name...

I also found this little tidbit which further substantiates my claim as well as the ksh portability you mentioned.

The declare or typeset builtins, which are exact synonyms, permit modifying the properties of variables. This is a very weak form of the typing [1] available in certain programming languages. The declare command is specific to version 2 or later of Bash. The typeset command also works in ksh scripts.

SiegeX
  • 120,826
  • 20
  • 133
  • 152
  • 11
    "help typeset" tells me that it's obsolete. Deprecated in favor of "declare". – lecodesportif Dec 12 '10 at 00:23
  • this is a good example of the problems in the bash documentation and theres inconsistency with command line help (using the 'help' command) and the manual pages, (using 'man bash' and going to SHELL BUILTINS) and the builtin page (using 'man builtins'). There are also some typos in there, so be careful – osirisgothra Oct 20 '13 at 05:42
  • 1
    "When used in a function, declare makes each name local, as with the local command, unless the -g option is used." from https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html – Roland Feb 28 '20 at 08:35
7

In the Bash manual under section 4.2 Bash Builtin Commands it states:

'typeset'
typeset [-afFrxi] [-p] [NAME[=VALUE] ...]
The 'typeset' command is supplied for compatibility with the Korn shell; however, it has been deprecated in favor of the 'declare' builtin command.

Dennis Williamson
  • 303,596
  • 86
  • 357
  • 418