|Previous||Table of Contents||Next|
The internal typeset command is used to assign attributes and values to shell variables. By assigning attributes to shell variables you can:
The following functions can be performed using the typeset command and attributes.
Following is the general format of the typeset command.
typeset [ -HLRZaefilprtux [ name[=value] ] ... ]
The following list describes the options and their arguments that may be used to control how typeset functions.
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.|
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.|
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).
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.
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.
Refer to the export and readonly commands described in modules 47 and 112. Both of these commands assign attributes to given variables.
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.
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.
|Previous||Table of Contents||Next|