Is there any difference between using typeset in ksh to simply setting a variable?
Question
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.
typeset TARGET="${XMS_HOME}/common/jxb/config/${RUNGROUP}.${ENV}.properties"
TARGET="${XMS_HOME}/common/jxb/config/${RUNGROUP}.${ENV}.properties"
Solution
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.
a=0
function x {
typeset a=1
}
x
echo $a
function y {
a=2
}
y
echo $a
will print
0
2
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.
OTHER TIPS
since shell scripting is a loosely typed language (in which variables wont have a datytype) we can use typeset to set a particular variable to take similar datatype of values only.