Understanding the Need for getopts
Before we do anything else, let’s review the difference between options and arguments.
- Options modify the behavior of a program or script.
- Arguments are the objects upon which a program or script will act.
The simplest way to demonstrate the difference is with the humble ls
command. If you want to see if a particular file is present, just use ls
and the filename as an argument, like this:
donnie@fedora:~$ ls coingecko.sh
coingecko.sh
donnie@fedora:~$
If you want to see the details about the file, you’ll need to add an option, like this:
donnie@fedora:~$ ls -l coingecko.sh
-rwxr--r--. 1 donnie donnie 842 Jan 12 13:11 coingecko.sh
donnie@fedora:~$
You see how the -l
option modifies the behavior of the ls
command.
You’ve already seen how to use normal positional parameters to pass options and arguments into a script. A lot of the time, this will work sufficiently well, and you won...