Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 5 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
1  
...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 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
another_value
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 –  sputnick 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 –  sputnick Feb 25 '13 at 21:52

Your Answer

 
discard

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.