Previous Table of Contents Next


Module 145
typeset (ksh)

DESCRIPTION

The internal typeset command is used to assign attributes and values to shell variables. By assigning attributes to shell variables you can:

*  Control how a variable is represented internally.
*  Control how a variable is displayed.
*  Set the access or scope of a variable.

The following functions can be performed using the typeset command and attributes.

*  Format your shell output using the -L, -R, and -Z attributes.
*  Perform rapid arithmetic using the -i attribute.
*  List all variables with specific attribute set.
*  List all variables set with an attribute.

COMMAND FORMAT

Following is the general format of the typeset command.

     typeset [ -HLRZaefilprtux [ name[=value] ] ... ]

Options

The following list describes the options and their arguments that may be used to control how typeset functions.


NOTE:  
Notice that options preceded with a - (hyphen) turn the type or attribute on. When the option is preceded by a + (plus), the type is turned off.



-H Provides UNIX to host-name file mapping on non-UNIX systems.
-Ln Left justify and remove leading blanks. If n is a positive number, set width of the variable to n. If n is not a positive number, set variable to width of the first value it is assigned. Each time the variable is assigned a value, the variable is padded with blanks on the right. If the value is too wide, it is truncated. The -R option is turned off. The -Z option causes leading zeros to be removed.
-Rn Right justify and fill with leading blanks. If n is a positive number, set width of the variable to n. If n is not a positive number, set variable to width of the first value it is assigned. Each time the variable is assigned a value, the variable is padded with blanks on the left. If the value is too long, it is truncated. The -L option is turned off.
-Zn Right justify and fill with leading zeros, only if the -L option has not been specified. If n is a positive number, set width of variable to n. If n is not a positive number, set variable to width of the first value it is assigned. Each time the variable is assigned a value, the variable is zero filled if the first nonblank character is a number. If the first character is not a number, the variable is blank filled. If value is too long, it is truncated.
-e The variable is tagged as having an error. You may set or unset this attribute. The shell does not use this attribute itself.
-f Defines the variable to be a function. The only other valid options are -t, -u, and -x. You cannot assign values to a function name. The -t option turns on execution tracing. The -u option marks the function as undefined. The FPATH variable is used to locate the function when it is referenced.
-in Variable is defined as an integer. If n is a positive number, it is used as the base for the variable. If n is not a positive number, the base from the first value assigned to the variable is used. By defining a variable as an integer, internal shell arithmetic is much faster (10 to 30 times).
-l Convert uppercase characters to lowercase. The uppercase option, -u, is turned off.
-p The output of typeset is written to the shell's two-way pipe. The pipe is established by ending a command with |&.
-r The variable is made read-only.
-t Tags the variable. You may define tags for the variable. The tags have no special meaning to the shell.
-u Convert lowercase characters to uppercase. The lowercase option, -l, is turned off.
-x The variable is exported.
If only the option is supplied and no arguments are given, then all variables with the attributes of the given option are listed.

Arguments

The following list describes the arguments that may be passed to the typeset command.

name=value Assigns the value to the variable referred to as name.
If no options or arguments are supplied, then all variables are listed with their attributes.

FURTHER DISCUSSION

The attributes or types assigned to variables are like those used in most programming languages. The C language has types of long, integer, and char to mention a few. These types tell the computer how to interpret the data stored in the variable.

There are two attributes you can set using commands other than typeset. The commands are the readonly command and the export command.

The attributes of a variable are initially set by the typeset command. The value assigned to a variable is transformed according to the variables attributes. For example, if you assign 10 to variable NVAR that is an octal integer (typeset -i8 NVAR), the value of NVAR would be transformed to 12 (octal).


NOTE:  
It is important to understand and remember how the shell handles attribute assignment. When you assign a value to a variable, the value is transformed based on the attributes of the variable.




NOTE:  
If you change the attributes of a variable, the value may change automatically. Check your results; don't assume you know the new value.



You can perform simple substring parsing with the formatting attributes. By setting the attributes of a variable to a certain length, you can assign a value to the variable so that only part of the value is retained. For example,

     cj> typeset -L  LSTR=abc
     cj> typeset -R5 RSTR

will set variable LSTR to abc, a three-position, left-justified string. RSTR is set to a five-position, right-justified string. If you performed the following assignments:

     cj> LSTR=/u1/tech/mylogin
     cj> echo $LSTR
     /u1

the result of /u1 is returned because LSTR only accepts the first three characters of a value. The following example shows how to right justify the value of a variable:

     cj> RSTR=x
     cj> echo ":$RSTR:"
     :    x:

You can remove the attributes (and the variable) by using the unset command. When you use unset you remove the variable from the environment.

RELATED COMMANDS

Refer to the export and readonly commands described in modules 47 and 112. Both of these commands assign attributes to given variables.

APPLICATIONS

You can benefit greatly from the use of the typeset command. You can format output to generate reports, tables, and menus. If you need to perform mathematical calculations, typesetting the variable to integer type will increase the execution speed of your shell scripts tremendously. The alphabetic case conversion provides an easy and fast method of converting between uppercase and lowercase.

The typeset command takes the shell programming language one step closer to a powerful high-level language, allowing you to control the data types related to variables, plus providing automatic text manipulation within variable assignment.

TYPICAL OPERATION

In this activity you use the typeset command to define zero filled numbers. If you are using the csh skip to the next module. Begin at the shell prompt.

1.  Type typeset -Z5 NUM and press Return. This defines NUM as a five-place variable. The output will be 5 places, zero filled.
2.  Now assign a value to NUM by typing NUM=4 and pressing Return.
3.  Display the NUM variable by typing echo $NUM and pressing Return. Notice that the 4 is preceded by four zeros (00004).
4.  Turn to Module 84 to continue the learning sequence.


Previous Table of Contents Next