- Variables
- Special Parameters
- Variable Expansion
- Array Variables
- Compound Variables
- Quoting
Special Parameters
Some special parameters are automatically set by the Korn shell and usually cannot be directly set or modified.
The ? Parameter
The ? parameter contains the exit status of the last executed command. In this example, the date command is executed. It ran successfully, so the exit status is 0:
$ date +%D 05/24/96 $ print $? 0
Notice that there was no output displayed from the date command. This is because the >&– I/O operator causes standard output to be closed. The next command, cp ch222.out /tmp, did not run successfully, so 1 is returned:
$ cp ch222.out /tmp ch222.out: No such file or directory $ print $? 1
When used with a pipe, $? contains the exit status of the last command in the pipeline.
Table 3.4. Some Preset Special Parameters
? |
exit status of the last command |
$ |
process id of the current Korn shell |
– |
current options in effect |
! |
process id of the last background command or co-process |
ERRNO |
error number returned by most recently failed system call (system dependent) |
PPID |
process id of the parent shell |
The $ Parameter
The $ parameter contains the process id of the current shell.
$ print $$ 178
It is useful in creating unique file names within Korn shell scripts.
$ touch $0.$$ $ ls *.* ksh.218
Other Special Parameters
The – parameter contains the current options in effect. The output of the next command shows that the interactive and monitor options are enabled:
$ print $— im
To display the error number of the last failed system call, use the ERRNO variable. Here, an attempt to display a non-existent file returns an error, so ERRNO is checked for the error number:
$ cat tmp.out tmp.out: No such file or directory $ print $ERRNO 2
This is system dependent, so it may not be available on your system. Check your documentation or /usr/include/sys/errno.h for more information.
Special Reserved Variables
The Korn shell has two types of reserved variables - those that are set and updated automatically by the Korn shell, and those that are set by each user or the system administrator. These are listed in detail in Chapter 7.