Main index

Introducing UNIX and Linux


Advanced shell programming

Overview
Sending and trapping signals
      Signal names
Functions
Aliases
The 'exec' mechanism
The 'eval' mechanism
Sending data across networks
      Sending printable characters
      Splitting files
Makefiles
Safe programming
Setting up a terminal
More on files
Miscellaneous utilities
Summary
Exercises

Answer to chapter 9 question 3

The body of this function is the same as a script, if you had written it as a script instead. It must be written as a function in order that the value of PATH in the current shell can be altered -- you cannot export from a child process to its parent.

addtopath() {
   printf "Enter directory name: "    # Prompt
   read NEW                           # Read name
   if [ -d "$NEW" ] &&                # Check directory
      [ -r "$NEW" ]                   # Check readable
   then PATH="$PATH":"$NEW"           # Update PATH
   fi
   }

Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck