Korn Shell Variables
Variables are set with the = operator as in Bourne shell. No space is allowed around the = operator. Variables may have attributes assigned by typeset with the following syntax:
| ___ Korn Shell ** typeset_ syntax** | |
|---|---|
| typeset -attrib(s) variable=[value] | assign attribute and optional value |
| typeset +attrib(s) variable | remove attribute |
| typeset | list all vars and attributes |
| typeset -attrib | list all vars with -attrib type |
| typeset +attrib | list only vars with +attrib type |
| List of typeset Supported Attributes | |
| -f | the name refers to a function |
| -H | system to hostname file mapping |
| -i | integer |
| -l | lower case |
| -L | left justify, remove leading spaces |
| -LZ | left justify, remove leading zeroes |
| -r | read only (cannot be removed) |
| -R | right justify |
| -RZ | right justify with leading zeroes |
| -t | tag named parameter |
| -u | upper case |
| -x | export |
| _ ** _** |
Any variable can be assigned the output of a command or of a file with the syntax:
| __ var=$(command) | Korn specific |
|---|---|
var=command | Bourne compatible |
var=$(< file) | Korn specific |
var=cat file | Bourne compatible |
| _ ** _** |
Variables are removed by the unset builtin. To define a variable that executes a script file using Ksh syntax, do the following:
> ksh
$ cv=$(<kls)
$ echo $cv
ls -al
$
$ $cv
total 5
drwxr-xr-x 3 john users 8192 Mar 10 10:48 .
drwxr-x--x 17 john users 8192 Mar 9 16:05 ..
-rw-r--r-- 1 john users 299 Mar 9 11:25 a.a
-rwxr-xr-x 1 john users 261 Mar 10 08:44 cread
-rw-r--r-- 1 john users 37 Jun 18 1998 er
_ ** ** ** Ksh** _ ** Special variables are:**
__ Korn Shell Special Variables
# | number of positional parameters
? | exit status
$ | process ID
- (dash) | current options
_ (underscore) | the last argument of the previous command
! | process ID of last background
ERRNO | error no. of last failed system call
LINENO | the line no. of the current script line in execution
OLDPWD | previous cd directory
OPTARG | last option processed by getopts
OPTIND | index of last option processed by getopts
PPID | parent PID
PWD | current directory
RANDOM | random funcion
REPLY | menu item no. in response to select Ksh command
SECONDS | seconds since shell invocation
CDPATH | search path for cd command
COLUMNS | edit window width
EDITOR | editor management
ENV | generate path name in tracked alias and functions
FCEDIT | default editor for history processing command fc
FPATH | search path for functions
IFS | internal field separator
HISTFILE | store command history file
HISTSIZE | history buffer size
HOME | home directory
LANG | system locale
LC_COLLATE | collating sequence
LC_CTYPE | character classification
LC_MESSAGES | language for system messages
LC_MONETARY | monetary format
LC_NUMERIC | numeric format
LC_TIME | date and time format
LINES | column length
LOGNAME | login name
MAIL | mail notify
MAILCHECK | mail notify interval
MAILPATH | mail notify
NLSPATH | search path for messages
PATH | searc path for commands
PS1 | primary promt
PS2 | secondary prompt
PS3 | selection prompt (default #?)
PS4 | trace prompt (default +)
SHELL | shell in use
TMOUT | command timeout to terminate shell
VISUAL | editor option
_ ** _**
** Variables are protected by braces:**
__
$ print Current options/ERRNO: ${-}, $ERRNO
Current options/ERRNO: ism, 10
_ ** _Metacharacters are printed when prefixed by\. **
__ Korn Shell Variable Usage and Setting Rules
varlen=${#var} | variable length var=${var1:-value} | var=var1 if var1 set, var=value if var1.not.set or empty var=${var1-value} | var=var1 if var1 set, var=value only if var1.not.set var=${var1:=var2} | var=$var1 if var1 set and not empty, else = $var2 var=${var1=var2} | var=$var1 if var1 set even if empty, else = $var2 var=${var1:+var2} | var=$var2 if var1 set and not empty, else var not set var=${var1:+var2} | var=$var2 if var1 set even if empty, else var not set var=${var1#var2} | var=var1 with smallest part of left matched var2 deleted var=${var1##var2} | var=var1 with largest part of left matched var2 deleted var=${var1%var2} | var=var1 with smallest part of right matched var2 deleted var=${var1%%var2} | var=var1 with largest part of right matched var2 deleted var=${var1:?} | var=var1 if var1 set else error message var=${var1:?var2} | var=var1 if var1 set else var=var2 and exit _ ** _**
Korn shell can handle arrays as ** C** _ ** shell but with a different syntax.**_
__ Korn Shell Array Syntax
arr[0]=val0 arr[1]=val1 ... | init array in any order
set -A arr val0 val1 ... | alternate init for ordered array
typeset arr[0]=val0 arr[1]=val1 ... | alternate init array in any order
${arr}, $arr | array element zero
${arr[n]} | array element n
${arr[n+2] | array element n+2
${arr[$i]} | array element $i
${arr[*]}, ${arr[@]} | all elements of array
${#arr[*]}, ${#arr[@]} | number of array elements
${#arr[n]} | length of array element n
_ ** _**
** Example of array usage:**
__
#!/bin/ksh
#-----------karray: arrays with Korn shell
#
echo Proc $0: arrays in Korn shell
echo
set -A rgb red green blue yellow magenta cyan
print rgb is a ${#rgb[*]} items color array with values:
print ${rgb[*]}
print 4-th item is ${rgb[1+3]} ${#rgb[4]}-bytes long
#
#----------end script------------------
_ ** The_ ** set +A _ ** statement allows partial redefinition of ordered array elements. Consider the following:**_
$ set -A rgb red green blue yellow magenta cyan
$ print ${rgb[*]}
red green blue yellow magenta cyan
If you use ** -A** _ ** to change only the first item of array_ ** rgb _ ** _, the array is truncated. If you use__ ** +A _ ** , the first items are changed keeping the remaining ones:**_
$ set -A rgb red green blue
$ print ${rgb[*]}
red green blue
$ set -A rgb red green blue yellow magenta cyan
$ set +A rgb rosso
$ print ${rgb[*]}
rosso green blue yellow magenta cyan
__
In Korn shell quotes have the same usage as in Bourne shell.
__
_single quotes_ '` hide meaning of special characters, do not perform variable substitution within quoted string
double quotes " preserve embedded spaces and newlines, set vars to
NULL , display single quotes, perform variable substitution back quotes ``` assign command output to vars `