ennuikiller ennuikiller - 1 year ago 50
Bash Question

Is there any difference between using typeset in ksh to simply setting a variable?

Are the following 2 lines completely equivalent? If not what's the difference? I've seen plenty of shell scripts utilize number 1 and was just wondering what it gives you compared with number 2.

  1. typeset TARGET="${XMS_HOME}/common/jxb/config/${RUNGROUP}.${ENV}.properties"

  2. TARGET="${XMS_HOME}/common/jxb/config/${RUNGROUP}.${ENV}.properties"

Answer Source

typeset will create a local variable (one which doesn't "leak"). This is useful in functions but I've also seen it being used at the top level of a shell script.

function x {
    typeset a=1
echo $a
function y {
echo $a

will print


You can also use typeset to create arrays and integers.

[EDIT] Added function keyword because some shells require it. Remove it if it offends your shell but it should work with most versions.