Ksh variables may contain either strings or integers. They are of type string by default. To declare an integer variable x, use typeset -i x' Use the normal ksh syntax to reference variables:
$x | Simple reference |
${x} | Simple reference, explicit bounding of name |
${x-val} | Substitute `val' if x is unset |
${x:-val} | Substitute `val' if x is unset or is null |
${x=val} | Assign `val' to x if x is unset |
${x:=val} | Assign `val' to x if x is unset or null |
${x+val} | Substitute `val' if x is set |
${x:+val} | Substitute `val' if x is set and non-null |
${x?val} | Generate error msg `val' if x is unset |
${x:?val} | Generate error msg `val' if x is unset or null |
${x#pat} | Value of x, less smallest left-matching pattern |
${x##pat} | Value of x, less largest left-matching pattern |
${x%pat} | Value of x, less smallest right-matching pattern |
${x%%pat} | Value of x, less largest right-matching pattern |
${#x} | Length of the (formatted) value of x |
Use typeset -ibase x to declare x an integer variable with output base base (2 to 36, inclusive). Numeric values in other bases may be entered as base#digits. For example, the number 42 (decimal) may be entered as any of the following:
2#101010 | 3#1120 | 4#222 | 5#132 |
6#110 | 7#60 | 8#52 | 9#46 |
10#42 | 11#39 | 12#36 | 13#33 |
14#30 | 15#2C | 16#2A | 17#28 |
18#26 | 19#24 | 20#22 | 21#20 |
22#1K | 23#1J | 24#1I | 25#1H |
26#1G | 27#1F | 28#1E | 29#1D |
30#1C | 31#1B | 32#1A | 33#19 |
34#18 | 35#17 | 36#16 |
Other valid forms for 42 are 052 and 0x2a (the C syntax for octal and hexadecimal constants, respectively). Case is not significant in any of these forms.