Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can anyone explain difference between declare and typeset with real life examples .

share|improve this question
up vote 12 down vote accepted

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.

share|improve this answer
can you please provide some example of where and why this use in bash script – Rahul Patil Feb 25 '13 at 8:54
...like I said, they are exactly the same. I don't understand how I can differentiate them if they are so. – Chris Down Feb 25 '13 at 10:04
Good one. Note that "obsolete" just appears in help typeset, not in man typeset. – fedorqui Jul 22 '14 at 8:05

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
share|improve this answer
This is a nice comment but how does that answer the question which is about typeset and declare? – jw013 Feb 25 '13 at 16:40
This is already well answered by Chris Down, my response is just a case where it's usefull – Gilles Quenot Feb 25 '13 at 17:31
Perhaps I wasn't clear. Comments should be comments, and answers should be answers. This is a comment, not an answer. – jw013 Feb 25 '13 at 20:32
My snippet wouldn't clear in a comment – Gilles Quenot Feb 25 '13 at 21:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.