Difference between terms: "option", "argument", and "parameter"?

43,627

Solution 1

A command is split into an array of strings named arguments. Argument 0 is (normally) the command name, argument 1, the first element following the command, and so on. These arguments are sometimes called positional parameters.

$ ls -la /tmp /var/tmp
arg0 = ls
arg1 = -la
arg2 = /tmp
arg3 = /var/tmp

An option is a documented1 type of argument modifying the behavior of a command, e.g. -l commonly means "long", -v verbose. -lv are two options combined in a single argument. There are also long options like --verbose (see also Using getopts to process long and short command line options). As their name suggests, options are usually optional. There are however some commands with paradoxical "mandatory options".

$ ls -la /tmp /var/tmp
option1= -l
option2= -a

A parameter is an argument that provides information to either the command or one of its options, e.g. in -o file, file is the parameter of the -o option. Unlike options, whose possible values are hard coded in programs, parameters are usually not, so the user is free to use whatever string suits his/her needs. Should you need to pass a parameter that looks like an option but shouldn't be interpreted as such, you can separate it from the beginning of the command line with a double dash: --2.

$ ls -la /tmp /var/tmp
parameter1= /tmp
parameter2= /var/tmp

$ ls -l -- -a
option1    = -l
parameter1 = -a

A shell parameter is anything that store a value in the context of the shell. This includes positional parameters (e.g. $1, $2...), variables (e.g. $foo, $bar...) and special character ones (e.g. $@)

Finally, there are subcommands, also known as functions / (low-level) commands, which are used with "metacommands" that embed multiple separate commands, like busybox, git, apt-get, openssl, and the likes. With them, you might have global options preceeding the subcommand, and subcommand specific options that follow the subcommand. Unlike parameters, the list of possible subcommands is hardcoded in the command itself. e.g.:

$ busybox ls -l
command            = busybox
subcommand         = ls
subcommand option1 = -l

$ git --git-dir=a.git --work-tree=b -C c status -s
command            = git
command option1    = --git-dir=a.git
command option2    = --work-tree=b
command option3    = -C c
subcommand         = status
subcommand option1 = -s

Note that some commands like test, tar, dd and find have more complex argument parsing syntax than the ones described previously and can have some or all of their arguments parsed as expressions, operands, keys and similar command specific components.

Note also that optional variable assignments and redirections, despite being processed by the shell for tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal like other command line parameters are not taken into account in my reply because they have disappeared when the command is actually called and passed its arguments.

1 I should have written usually documented because of course, undocumented options are still options.
2 The double dash feature need to be implemented by the program though.

Solution 2

The man page for a typical Unix command often uses the terms argument, option and parameter. At the lowest level, we have argument and everything is an argument, including the (filesystem path to the) command itself.

In a shell script you access arguments using the special variables $0 .. $n. Other languages have similar ways to access them (commonly through an array with a name like argv).

Arguments may be interpreted as options if you wish. How this is done is implementation-specific. You can either roll your own, for exampe a shell (such as bash) script can use provided getopts or getopt commands.

These typically define an option as an argument beginning with a hyphen (-) and some options may use proceeding arguments as its parameters. More capable parsers (e.g getopt) support mixing short-form (-h) and long-form (--help) options.

Typically, most options take zero or one parameter. Such parameters are also sometimes called values.

The supported options are coded in the program code (e.g in the invocation of getopts within a shell script). Any remaining arguments after the options have been consumed are commonly called positional parameters when the order in which they are given is significant (this is in contrast to options which usually can be given in any order).

Again, the script defines what the positional parameters are by how it consumes and uses them.

So a typical command

$ ls -I README -l foo 'bar car' baz

has seven arguments: /usr/bin/ls, -I, README, -l, foo, bar car, and baz accessible as $0 thru $6. The -l and -I are interpreted as options, the latter having a parameter (or value) of README. What remains are positional parameters (foo, bar car and baz).

Option parsing may alter the argument list by removing those it consumes (e.g using shift or set) so that only the positional parameters remain and are thereafter accessible as $1 .. $n.

Solution 3

Since the question is tagged "bash", I looked for relevant sections in the Bash manual. I list these as quoted passages below together with my own one sentence summaries.

Arguments

Everything following the command is an argument.

A simple shell command such as echo a b c consists of the command itself followed by arguments, separated by spaces.

A simple command is the kind of command encountered most often. It’s just a sequence of words separated by blanks, terminated by one of the shell’s control operators (see Definitions). The first word generally specifies a command to be executed, with the rest of the words being that command’s arguments.

Parameters

Arguments are referred to as parameters during function execution.

When a function is executed, the arguments to the function become the positional parameters during its execution

A parameter is an entity that stores values. It can be a name, a number, or one of the special characters listed below. A variable is a parameter denoted by a name.

A positional parameter is a parameter denoted by one or more digits, other than the single digit 0. Positional parameters are assigned from the shell’s arguments when it is invoked, and may be reassigned using the set builtin command. Positional parameter N may be referenced as ${N}, or as $N when N consists of a single digit.

Options

There is no dedicated section to defining what an option is, but they are referred to as hyphen-prefixed characters throughout the manual.

The -p option changes the output format to that specified by POSIX

Share:
43,627
what is what
Author by

what is what

Updated on April 24, 2021

Comments

  • what is what
    what is what about 3 years

    What are the differences between these terms: "option", "argument", and "parameter"? In man pages these terms often seem to be used interchangeably.

  • user1534664
    user1534664 almost 7 years
    @jlliagre Are "hardcoded variables" known as parameters too? E.g. some path to a file, which u dont pass as an argument, but create inside the bash script, using VARNAME="VALUE" syntax. If not (i.e. VARNAME is not a parameter), then why does the concept of parameter expansion apply to hardcoded variables as well?
  • jlliagre
    jlliagre almost 7 years
    @user1534664 I left these optional variable assignments outside the scope of my reply as they are not seen by the executed command as arguments. I didn't take either into account redirections for the same reason but you are right that from the shell interpreter point of view, these assignments are preprocessed for tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal like other parameters.
  • joelostblom
    joelostblom over 4 years
    @jlliagre Do you have a reference for the definitions you gave above? When I looked these terms up in the bash manual, the definitions were slightly different from yours. However, the manual did not specifically mention concepts such as long options, so maybe there is a better reference?
  • jlliagre
    jlliagre over 4 years
    @joelostblom Answer updated with some links, including to a couple of references about short/long options.
  • CopsOnRoad
    CopsOnRoad about 3 years
    @jlliagre Beautiful examples and correct me if I'm wrong, in your last example $ git --git-dir=a.git --work-tree=b -C c status -s, c is also a sub-command. You can edit your answer to include it.
  • jlliagre
    jlliagre about 3 years
    @CopsOnRoad c is actually part of the third option (its parameter), answer fixed. Thanks!
  • jlliagre
    jlliagre about 3 years
    @CopsOnRoad There is a space. The fact is git option -c requires a parameter. You can't guess what is what just by looking to a command line. The command documentation will tell what is expected. There might be commands supporting consecutive subcommands, that might be a sub-command then a sub-sub-command. Commands developers a free to decide about it.
  • The Quark
    The Quark over 2 years
    "Arguments are referred to as parameters during function execution." This is not entirely accurate, "parameter" is not just an other name for "argument". It is rather that arguments are stored in parameters (more precisely, in positional parameters). More generally, as written in the quoted section, parameters are entities that store values. Arguments are options, option-arguments, or operands following the command name in a command line (see here).
  • Pryftan
    Pryftan about 2 years
    Not that it matters much but technically an option is still an option even if it's not documented. The same can go for the others as well. Also as for the -- to disable options: that depends on the program. It might be that it wasn't programmed to support this but many tools do use this. Great answer though and good call on bringing up how some programs have required options.