5

Both a positional parameter ($1, $2, and so forth) and an option (and/or argument) are both written directly after a command, so what is the definition or phrasing to explain how to distinct them?

In other words, how to formally explain the difference between a positional parameter and an option (and/or argument)?

11

An option (also commonly called "flag" or "switch") is one type of command line argument. A command line argument is a single word (or quoted string) present on the command line of a utility or shell function.

Upon calling a shell script or shell function with a certain number of arguments, each individual argument will be available as a positional parameter inside the script or function.

Terminology:

An "argument" can be

  • an "option" (like -a),
  • an "option-argument" (like foo in -a foo if -a takes an argument), or
  • an "operand" (a non-option argument that is also not an option-argument, for example foo in -a foo if -a does not take an option-argument).

Real example of all of the above (using GNU mv):

mv -t targetdir -f file1 file2
  • Arguments: -t, targetdir, -f, file1, and file2
  • Options: -t and -f
  • Option-arguments: targetdir
  • Operands: file1 and file2.

From the POSIX definitions:

[An argument is, in] the shell command language, a parameter passed to a utility as the equivalent of a single string in the argv array created by one of the exec functions. An argument is one of the options, option-arguments, or operands following the command name.

[An option is an] argument to a command that is generally used to specify changes in the utility's default behavior.

[An option-argument is a] parameter that follows certain options. In some cases an option-argument is included within the same argument string as the option-in most cases it is the next argument.

[An operand is an] argument to a command that is generally used as an object supplying information to a utility necessary to complete its processing. Operands generally follow the options in a command line.

The positional parameters in a shell script or shell function will be the arguments given on the script's or function's command line, regardless of whether the arguments are options, option-arguments or operands.

The positional parameters may also be set using

set -- something "something else" bumblebees

This sets $1, $2 and $3 to the three strings and clears any other positional parameters.

In this case, the positional parameters no longer have any relation to the arguments passed on the utility's command line.

See also:

  • Interesting, from looks on online literature I thought that in Bash an option always precedes an argument (if there is an option in the first place). What you describe is new to me. – JohnDoea Nov 26 '18 at 10:03
  • 2
    @JohnDoea An option is an argument. "Argument" is a generic term for a word or quoted string on the command line. This includes words like -a (which is probably an option depending on how the utility interprets it and where it occurs in the command line). – Kusalananda Nov 26 '18 at 10:05
  • 2
    @JohnDoea The command interprets the argument how it wishes. There are some common conventions, but they are just that: conventions. Plenty of tools do not follow them. For example you generally expect that options can be specified in any order (why would it matter if I tell you to be --verbose while --print-numbers instead of --print-numbers being --verbose?), but in a lot of cases that's not true. So options are just a rule of thumb that works for many common utilities but there are really only arguments and commands that interpret them how they like. – Giacomo Alzetta Nov 26 '18 at 14:40
  • @Kusalananda $1, $2 and $3 --- these strings, are they considered arguments or operands? That I miss from the answer (btw I now say to myself: Every positional parameter is an argument but not every argument is a positional parameter). – JohnDoea Nov 27 '18 at 16:06
  • @JohnDoea Every argument is assigned to a positional parameter. I don't want to say "is a positional parameter" as they are arguments on the command line that are transferred into the script and made available through the positional parameters. The reverse (a positional parameter is an argument) may not be true if the positional parameters are set within the script with set as I showed. – Kusalananda Nov 27 '18 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.