Passing your program flags

After going into your shell and opening a new file in your favorite editor, let's get started by creating a Bash script that does the following:

  • When no flags or arguments are specified, prints out a help message
  • When either the -h or --help flags are set, it prints out a help message
  • When the -f or --firstname flags are set, it sets the the first name variable
  • When the -l or --lastname flags are set, it sets the the last name variable
  • When both the firstname and lastname flags are set, it prints a welcome message and returns without error

In addition to the basic logic, we can see that the code leverages a piece of functionality called getopts. Getopts allows us to grab the program parameter flags for use within ...

Get Bash Cookbook 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.