1756724 Members
2341 Online
108852 Solutions
New Discussion юеВ

Re: typeset usage

 
Sangeeta Gupta
Occasional Contributor

typeset usage

Hi,

I am working on code porting from tru64 to hp-ux.
I want to know what would i benefit if i use a typeset to variable before i use it.
or rather when should we use variables after doing typeset for it and when to use it without doing a typeset.
Also does it have different impact on different unix systems.
Thanks and Regards,
Sangeeta
8 REPLIES 8
Peter Godron
Honored Contributor

Re: typeset usage

Sangeeta,
I would always be careful useing typeset variables and have not found a definite need for them, as yet.

Example:
# typeset -i a
# a=0.4
# echo $a
0
# b=0.4
# echo $b
0.4
#

James R. Ferguson
Acclaimed Contributor

Re: typeset usage

Hi:

Using 'typeset' for your variables allows you to do:

1. Faster arithmetic (if declared a s an integer).

2. To perform automatic upper/lower case translations when you store into the variable, eliminating testing for "Y" or "y" or "N" or "n", for example.

3. To right or left justify and fill will blanks or zeros.

4. Declare read-only variables so that you can logically treat them as constants.

5. Create local variables within subroutines.

These implementions are common among the POSIX, ksh, and bash shells. Obviously, there is great benefit to using 'typeset'.

If you code in a C or Perl, 'typeset' feels like a "natural" thing to do.

Regards!

...JRF...
Ralph Grothe
Honored Contributor

Re: typeset usage

Apart from what James already has mentioned,
probably the biggest asset of typesetting shell variables is that it lets you scope variables to avoid inadvertent name clashes like in proper programming languages (even untyped ones like Perl, otherwise no CPAN was possible at all).

You may already have noticed these neat alii that HP compiled into their sh:

$ alias autoload functions integer local
autoload='typeset -fu'
functions='typeset -f'
integer='typeset -i'
local=typeset

So you could use typeset (or the aliased local) for local scoping in function definitions like this:

$ scope() { var=blah;echo $var; };scope;echo ${var:-unset};unset -f scope;unset var
blah
blah

$ scope() { typeset var=blah;echo $var; };scope;echo ${var:-unset};unset -f scope;unset var
blah
unset

You can also temporarily export variables
whithout much ado

$ var=blah printenv var;printenv var;printenv var;unset var
blah

I like the auto case conversion already mentioned by James.

$ typeset -u uc_pwd=$PWD; echo $uc_pwd
/ETC/RC.CONFIG.D

likewise in lower case

$ typeset -l lc_pwd=$uc_pwd;echo $lc_pwd
/etc/rc.config.d

Integer arithmetics are accomplished without fussy bracing etc.

$ typeset -i i=0;while ((i<=10));do a[i+=1]="$((10-i))\n";done;echo ${a[*]}
10
9
8
7
6
5
4
3
2
1
0


You can also declare read only variables which may act similar to constants.

$ typeset -r const="won't change";const="take this"
const: This variable is read only.

Enough reasons I would think to more often make use of typeset.

Madness, thy name is system administration
A. Clay Stephenson
Acclaimed Contributor

Re: typeset usage

The major portability stubling block that I have found involves the typeset -i syntax. Some implementations allow this construct:
typeset -i8 OCTALVAR=1
so that a base/radix can be declared rather than the default base10.

typeset -i VAR=1 works everywhere (Korn, POSIX, bash, zsh) but typeset -i10 works in some implementations but not others. I would avoid integer declaration with a base specification although that can be an extremely useful construct in some cases.

In any event, typeset should be used for exactly the same reasons that strongly typed languages are favored. If you use them nowhere else, at least use them within functions so that you can use local variables without fear of side-effects.
If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: typeset usage

>Clay: Some implementations allow this construct:
typeset -i8 OCTALVAR=1
so that a base/radix can be declared rather than the default base10.

Yes, we allow that. Also if you want to convert from hex:
typeset -i10 fromhex=16#dead; echo $fromhex
A. Clay Stephenson
Acclaimed Contributor

Re: typeset usage

Yes Dennis, I'm well aware of that. I was merely trying to point out that while the "typeset -i" works everywhere the "typeset -iN" is not very portable (though useful).
If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: typeset usage

>Clay: I was merely trying to point out that while the "typeset -i" ...

Yes, I understood that but wanted to mention what use you could do with bases.
Peter Godron
Honored Contributor

Re: typeset usage

Sangeeta,
you have had quite a few answers here.

If this problem is resolved, could you please complete the thread by awarding points to helpful answers and summarising the solution for you.

This will help resolution of similar problems in the future.