6

I've found this example script for using getopt command in shell.

#!/bin/bash
args=$(getopt ab $*)
set -- $args
for i;
do
  case "$i" in
    -a)shift; echo "it was a";;
    -b)shift; echo "it was b";;
  esac;
done

It works well, but I don't understand where is variable $i assigned. How it knows that it must iterate through $arg. Can you explain this?

rubo77
  • 17,707
  • 27
  • 124
  • 213
sev3ryn
  • 1,025
  • 1
  • 9
  • 16

1 Answers1

11

As shown here, for defaults to $@ if no in seq is given. The for i assigns your $i variable.

d33tah
  • 10,055
  • 12
  • 60
  • 142