next up previous contents
Next: C Shell Programming Up: Shell Programming Previous: The break and continue   Contents

Subprograms

As shell programs grow in size it eventually becomes necessary to impose some global structure by means of subprograms. They can be passed arguments and access them in the same way as the main script. The subprogram is defined by the construct:-

subprogram_name() {
 
  .....
  return
}

there can be other return statements. It is invoked by:-

subprogram_name arg_list

Here is a trivial example:-

#!/bin/bash
 
mysub() {
  echo "I got passed: $1"
  return
}
 
mysub abc
mysub def
 
exit 0

which when run produces the result:-

I got passed: abc
I got passed: def



P.D. Gronbech (IT Staff) 2015-10-02