What is Korn Shell typeset Statement
Variable Types
In the Bourne shell, there are two variable types: strings and constants. In the Korn shell, there are four variable types: strings, integers, arrays, and constants.
- All variables are of type string unless explicitly declared otherwise.
- You can specify constants in a shell. A constant is a read-only variable that was assigned a value when it is declared and the value cannot be changed.
- A variable data type determines the values that can be assigned to the variable.
You can assign any value to a string variable. Integer variables can hold only numeric values, and arrays might be restrictive, depending on whether an array is an array of integers or strings.
Assessing Variable Values
When you access the value of a variable by preceding its name with a $, you might need to isolate the variable name from the characters that immediately follow it; for example:
$ flower=rose
$ print "$flower $flowers $flowerbush"
rose
$ print "$flower ${flower}s ${flower}bush"
rose roses rosebush
The Korn Shell typeset Statement
The typeset statement sets variable attributes. In some cases, it changes a variable value (for example, a right- or left-justification). The typeset statement options are shown in the table below.
Syntax | Description |
---|---|
typeset -u var | Converts var to all uppercase characters |
typeset -l var | Converts var to all lowercase characters |
typeset -LZ var | Strips leading zeros from the string var |
typeset-Lnumvar | Left-justifiesvarwithinthefieldwidthspecifiedbynum |
typeset-Rnumvar | Right-justifiesvarwithinthefieldwidthspecifiedbynum |
typeset -i var | Specifies that var can only contain integer values |
typeset -r var | Specifies that var is read-only. Its value can’t be changed by subsequent assignment |
Example of Using String Manipulations
Quote the variable in the output statement to see a right-justified string with leading spaces (see example below):
$ cat strman1.ksh
#!/bin/ksh
# Script name: strman1.ksh
typeset -R8 word="happy"
typeset -L5 word1="depressed"
print "123456789"
print "$word"
print
print "123456789"
print "$word1"
$ ./strman1.ksh
123456789
happy
123456789
depre
Without quotes, leading spaces aren’t printed (see the following example):
$ cat strman2.
ksh #!/bin/ksh
# Script name: strman2.ksh
typeset -R8 word="happy"
typeset -L5 word1="depressed"
print "123456789"
print $word
print
print "123456789"
print $word1
$ ./strman2.ksh
123456789
happy
123456789
depre
The following script further demonstrates string manipulations. The script converts all characters in a string to uppercase and then lowercase characters. The script then left- and right-justifies the characters within the string. The line that prints 123456789 is included so you can check the output with regard to the field widths assigned to the variables ljust and rjust.
$ cat strman3.ksh
#!/bin/ksh
# Script name: strman3.ksh
string1="manipulation"
print "Length of string1 is ${#string1} characters \n"
string2="CaSes"
print "string2 is $string2"
typeset -u string2
print "string2 in upper case: $string2"
typeset -l string2
print "string2 in lower case: $string2 \n"
typeset -L7 ljust
ljust="hi there"
typeset -R5 rjust
rjust="farewell"
print " 123456789"
print "Value of ljust: $ljust"
print "Value of rjust: $rjust \n"
lzero="00034;lsl"
print "Value of lzero: $lzero"
typeset -LZ lzero
print "New value of lzero: $lzero"
$ ./strman3.ksh
Length of string1 is 12 characters
string2 is CaSes
string2 in upper case: CASES
string2 in lower case: cases
123456789
Value of ljust: hi ther
Value of rjust: ewell
Value of lzero: 00034;lsl
New value of lzero: 34;lsl
Declaring an Integer Variable
Declare an integer variable in two ways by using either typeset -i or integer in front of the variable name. The first way is more flexible because it also allows you to specify the base in which the variable is represented.
typeset -i int_var1[=value] int_var2[=value] ... int_varn[=value]
integer int_var1[=value] int_var2[=value] ... int_varn[=value]
An integer variable can have only integer numbers assigned to it. An assignment of a number with a decimal part results in the decimal being truncated. An assignment containing nonnumeric characters to an integer variable results in an error message.
$ typeset -i num # base 10 integer
$ num=5
$ print $num
5
$ typeset -i num # base 10 integer
$ num=25.34 # truncation will occur on the decimal portion
$ print $num
25
$ typeset -i num # base 10 integer
$ num=27
$ print $num
27
$ typeset -i8 num # change to base 8
$ print $num
8#33
$ num=two
/usr/bin/ksh: two: bad number
$ print $num
8#33