Scripting
12 minArguments and shift
Reading a script's own parameters without losing one.
set -- replaces the positional parameters, which is how you can practise a script's argument handling without writing a script.
for f in "$@" is the only spelling of the loop that survives an argument containing a space. for f in $@ re-splits, and for f in $* joins first and then splits.
Try this
shift drops the first parameter and fails when there is nothing left, which is what makes the usual argument loop terminate.
Try this
getopts is the parser to use for flags. It reads one option per call — which is why it is always written as a while loop — setting the variable you name to the letter, and OPTARG to the value for the letters your option string marked with a colon.
OPTIND counts arguments, so the loop is followed by shift $((OPTIND - 1)). That line is the one people leave out, and without it a script goes on to treat its own flags as filenames.
A colon at the start of the option string turns off its error messages and hands you the offending letter in OPTARG instead — which is the only way to write your own.
Try this
Files
| Mode | Links | Size | Name |
|---|---|---|---|
| -rw-r--r-- | 1 | 97 | invoice.md |
| -rw-r--r-- | 1 | 62 | manifest.txt |