Previous Table of Contents Next


CHAPTER 17
Reference Tables and Example Scripts

Reference Tables
Example Scripts

Reference Tables

THIS CHAPTER CONTAINS TABLES OF SYNTAX ELEMENTS FOR ALL THREE SHELLS AND examples of shell scripts.

Environment Files

Feature Bourne Korn C
Read at login .profile .profile .login
Read at invocation of shell Any file specified in .profile with ENV=.file .cshrc

First Line of Script

Shell Syntax
Bourne #!/bin/sh
Korn #!/bin/ksh
C #!/bin/csh -f

Korn Shell Path Operators

Operator Description
${variable# pattern} Delete the shortest part at the beginning of the variable that matches the pattern and return the rest.
${variable## pattern} Delete the longest part at the beginning of the variable that matches the pattern and return the rest.
${variable% pattern} Delete the shortest part at the end of the variable that matches the pattern and return the rest.
${variable%% pattern} Delete the longest part at the end of the variable that matches the pattern and return the rest.

C Shell Path Modifiers

Modifier Meaning Description
:e Extension Remove prefix ending with a dot.
:h Head Remove trailing path name component.
:r Root Remove suffixes beginning with a dot (.).
:t Tail Remove all leading pathname components.
:q Quote Force variable to be quoted. Used to quote $argv.
:x Like q, but break into words at each space, tab, or newline.

Variables Initialized by Shell

Variable Explanation
$* Bourne or Korn shell: List the value of all command-line parameters. This variable is useful only in scripts because the login shell has no arguments associated with it.
C shell: Not used. Use $argv instead.
$# Bourne or Korn shell: Return the number of command-line arguments (in decimal). Useful only in scripts.
C shell: Count the number of words (in decimal) in a variable array.
$? Bourne or Korn shell: Return the exit status (in decimal) of the last command executed. Most commands return a zero exit status if they complete successfully; otherwise, a non-zero exit status is returned. This variable is set after each command is executed.
C shell: Check to see if a variable of that name has been set.
$$ All shells: Return the process ID (PID) number of the current shell (in decimal).
$! All shells: Return the process number (in decimal) of the last process run in the background.


Previous Table of Contents Next