Flags with arguments

In an optstring, the colon has an extra meaning beyond turning off verbose error logging: when placed after a letter, it signals to getopts that an option argument is expected.

If we look back at our first example, the optstring was simply :v. If we wanted the -v flag to accept an argument, we would place a colon behind the v, which would result in the following optstring: :v:. We can then use a special variable we've seen before, OPTARG, to grab that option argument.

We'll make a revision to our single-flag.sh script to show you how this works:

reader@ubuntu:~/scripts/chapter_15$ vim single-flag.sh reader@ubuntu:~/scripts/chapter_15$ cat single-flag.sh #!/bin/bash###################################### Author: Sebastiaan ...

Get Learn Linux Shell Scripting - Fundamentals of Bash 4.4 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.