How to distinguish between a positional parameter and an option?
up vote
6
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
6
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
6
down vote
favorite
up vote
6
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 2 days ago
justinnoor.io
274214
274214
asked 2 days ago
JohnDoea
551132
551132
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
2 days ago
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
2 days ago
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
2 days ago
@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
yesterday
@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
yesterday
|
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
2 days ago
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
2 days ago
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
2 days ago
@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
yesterday
@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
yesterday
|
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
2 days ago
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
2 days ago
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
2 days ago
@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
yesterday
@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
yesterday
|
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 2 days ago
answered 2 days ago
Kusalananda
117k16221360
117k16221360
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
2 days ago
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
2 days ago
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
2 days ago
@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
yesterday
@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
yesterday
|
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
2 days ago
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
2 days ago
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
2 days ago
@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
yesterday
@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
yesterday
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
2 days ago
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
2 days ago
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
2 days ago
@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
2 days ago
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
2 days ago
@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
2 days ago
@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
yesterday
@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
yesterday
@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
yesterday
@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
yesterday
|
show 1 more comment
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