Chapter 20. Passing parameters to scripts

We have already seen how to pass parameters to scripts by using the special variables $1..$9. Using $# can tell us how many parameters have been passed. We also know how to construct a usage statement, which is when we need to inform the user on how to call a script or function with its proper calling parameters.

In this chapter we will cover:

  • shift;

  • getopts; and

  • shift and getopts examples.

To recap, here’s a framework of a script that handles the parameters start and stop. The script requires two parameters. If it does not get two parameters then a usage statement is produced. Notice we use the case statement to process the different parameters coming into the script.

					$ pg opt #!/bin/sh # opt usage() ...

Get Linux and Unix Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.