How to distinguish between a positional parameter and an option?
up vote
5
down vote
favorite
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)?
shell-script variable arguments options parameter
add a comment |
up vote
5
down vote
favorite
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)?
shell-script variable arguments options parameter
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
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)?
shell-script variable arguments options parameter
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)?
shell-script variable arguments options parameter
shell-script variable arguments options parameter
edited Nov 26 at 15:12
justinnoor.io
340217
340217
asked Nov 26 at 9:46
JohnDoea
641132
641132
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
11
down vote
accepted
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
, andfile2
- Options:
-t
and-f
- Option-arguments:
targetdir
- Operands:
file1
andfile2
.
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:
- Confusion about changing meaning of arguments and options, is there an official standard definition?
- What is a "non-option argument"?
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 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 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 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 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 withset
as I showed.
– Kusalananda
Nov 27 at 16:16
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
accepted
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
, andfile2
- Options:
-t
and-f
- Option-arguments:
targetdir
- Operands:
file1
andfile2
.
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:
- Confusion about changing meaning of arguments and options, is there an official standard definition?
- What is a "non-option argument"?
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 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 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 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 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 withset
as I showed.
– Kusalananda
Nov 27 at 16:16
|
show 1 more comment
up vote
11
down vote
accepted
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
, andfile2
- Options:
-t
and-f
- Option-arguments:
targetdir
- Operands:
file1
andfile2
.
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:
- Confusion about changing meaning of arguments and options, is there an official standard definition?
- What is a "non-option argument"?
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 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 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 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 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 withset
as I showed.
– Kusalananda
Nov 27 at 16:16
|
show 1 more comment
up vote
11
down vote
accepted
up vote
11
down vote
accepted
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
, andfile2
- Options:
-t
and-f
- Option-arguments:
targetdir
- Operands:
file1
andfile2
.
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:
- Confusion about changing meaning of arguments and options, is there an official standard definition?
- What is a "non-option argument"?
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
, andfile2
- Options:
-t
and-f
- Option-arguments:
targetdir
- Operands:
file1
andfile2
.
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:
- Confusion about changing meaning of arguments and options, is there an official standard definition?
- What is a "non-option argument"?
edited Nov 26 at 14:20
answered Nov 26 at 9:55
Kusalananda
119k16223365
119k16223365
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 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 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 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 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 withset
as I showed.
– Kusalananda
Nov 27 at 16:16
|
show 1 more comment
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 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 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 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 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 withset
as I showed.
– Kusalananda
Nov 27 at 16:16
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 at 10:03
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 at 10:03
2
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 at 10:05
@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 at 10:05
2
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 at 14:40
@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 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 at 16:06
@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 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 at 16:16
@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 at 16:16
|
show 1 more comment
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f484152%2fhow-to-distinguish-between-a-positional-parameter-and-an-option%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown