Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
howto:getopts_tutorial [2015/02/12 06:37]
ormaaj Copy editing
howto:getopts_tutorial [2015/06/09 22:07] (current)
ogryzek Correct typo: change "an" to "and"
Line 24: Line 24:
 These are all positional parameters, but they can be divided into several logical groups: These are all positional parameters, but they can be divided into several logical groups:
   * ''​-x''​ is an **option** (aka **flag** or **switch**). It consists of a dash (''​-''​) followed by **one** character.   * ''​-x''​ is an **option** (aka **flag** or **switch**). It consists of a dash (''​-''​) followed by **one** character.
-  * ''​-f''​ is also an option, but this option has an associated **option argument** (an argument to the option ''​-f''​):​ ''/​etc/​mybackup.conf''​. The option argument is usually the argument following the option itself, but that isn't mandatory. Joining the option ​an option argument into a single argument ''​-f/​etc/​mybackup.conf''​ is valid.+  * ''​-f''​ is also an option, but this option has an associated **option argument** (an argument to the option ''​-f''​):​ ''/​etc/​mybackup.conf''​. The option argument is usually the argument following the option itself, but that isn't mandatory. Joining the option ​and option argument into a single argument ''​-f/​etc/​mybackup.conf''​ is valid.
   * ''​-r''​ depends on the configuration. In this example, ''​-r''​ doesn'​t take arguments so it's a standalone option like ''​-x''​.   * ''​-r''​ depends on the configuration. In this example, ''​-r''​ doesn'​t take arguments so it's a standalone option like ''​-x''​.
   * ''​./​foo.txt''​ and ''​./​bar.txt''​ are remaining arguments without any associated options. These are often used as **mass-arguments**. For example, the filenames specified for ''​cp(1)'',​ or arguments that don't need an option to be recognized because of the intended behavior of the program. POSIX(r) calls them **operands**.   * ''​./​foo.txt''​ and ''​./​bar.txt''​ are remaining arguments without any associated options. These are often used as **mass-arguments**. For example, the filenames specified for ''​cp(1)'',​ or arguments that don't need an option to be recognized because of the intended behavior of the program. POSIX(r) calls them **operands**.
Line 64: Line 64:
 |[[syntax:​shellvars#​OPTIND|OPTIND]]|Holds the index to the next argument to be processed. This is how ''​getopts''​ "​remembers"​ its own status between invocations. Also useful to shift the positional parameters after processing with ''​getopts''​. ''​OPTIND''​ is initially set to 1, and **needs to be re-set to 1 if you want to parse anything again with getopts**| |[[syntax:​shellvars#​OPTIND|OPTIND]]|Holds the index to the next argument to be processed. This is how ''​getopts''​ "​remembers"​ its own status between invocations. Also useful to shift the positional parameters after processing with ''​getopts''​. ''​OPTIND''​ is initially set to 1, and **needs to be re-set to 1 if you want to parse anything again with getopts**|
 |[[syntax:​shellvars#​OPTARG|OPTARG]]|This variable is set to any argument for an option found by ''​getopts''​. It also contains the option flag of an unknown option.| |[[syntax:​shellvars#​OPTARG|OPTARG]]|This variable is set to any argument for an option found by ''​getopts''​. It also contains the option flag of an unknown option.|
-|[[syntax:​shellvars#​OPTERR|OPTERR]]|(Values 0 or 1) Indicates if Bash should display error messages generated by the ''​getopts''​ builtin. The value is initialized to **1** on every shell startup - so be sure to always set it to **0** if you don't want to see annoying messages!|+|[[syntax:​shellvars#​OPTERR|OPTERR]]|(Values 0 or 1) Indicates if Bash should display error messages generated by the ''​getopts''​ builtin. The value is initialized to **1** on every shell startup - so be sure to always set it to **0** if you don't want to see annoying messages! ​**''​OPTERR''​ is not specified by POSIX for the ''​getopts''​ builtin utility --- only for the C ''​getopt()''​ function in ''​unistd.h''​ (''​opterr''​).** ''​OPTERR''​ is bash-specific and not supported by shells such as ksh93, mksh, zsh, or dash. |
  
 ''​getopts''​ also uses these variables for error reporting (they'​re set to value-combinations which arent possible in normal operation). ''​getopts''​ also uses these variables for error reporting (they'​re set to value-combinations which arent possible in normal operation).
- 
 ==== Specify what you want ==== ==== Specify what you want ====